Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this section, we’re going to connect our tables to our models using Eloquent relationships for our authors and books inside of the App directory. For example, if an author has several books; then, using an Eloquent relationship allows us to display all of the books by a specific author. Let’s get started.
Database Schema
Welcome back.
0:00
We created our models using
the php artisan make:model command
0:01
with the handy-m flag to create both
a model and migration at the same time.
0:06
Next, we're going to connect our tables
to our models using the Eloquent
0:14
relationships for our authors and
books inside of the app directory.
0:19
For example, if an author has
several books, then using Eloquent
0:25
relationship allows us to display all
of the books by a specific author.
0:30
Let's get started.
0:35
First, let's navigate to the app directory
and open the authors'.php model and
0:38
assign a variable using the protected
access modifier like this.
0:44
This command is not necessary because
Laravel automatically makes a connection
0:49
from the plural authors' table
to the singular author model.
0:54
The reason we're using this command is to
be explicit about what is happening here.
0:59
Otherwise, they would be, well, invisible.
1:04
So let's keep it for clarity's sake.
1:07
Next, let's define the relationship
between authors and books like this.
1:10
When we add the public function, Laravel
looks at the model defining the migration
1:35
and notices that we have an author_id
column inside of the database table.
1:41
Laravel then makes the connection that
$this author, singular, has many books.
1:48
Assuming that the author has
more than one book, of course.
1:54
You may be asking yourself,
if defining authors is optional.
1:59
Why are we covering this?
2:03
Which is a great question.
2:05
Imagine we have a database
that was pre-built for
2:07
us without using this best practice.
2:10
In such a case,
we would need to be explicit and
2:13
define the relationship using the foreign
key and local key in your application.
2:16
Since we're building the database
using best practices,
2:23
we can keep our code minimal.
2:27
To learn more about foreign keys and local
keys, check out the teacher's notes below.
2:29
Next, let's define the book model and
follow the same best practices.
2:36
As we discussed, we don't need
to define the dollar table as
2:41
books because it's the plural
of the book model.
2:44
But I like to be explicit
when building models.
2:48
So let's use protected $table = 'books'
like we did in the author model.
2:51
The same explicit relationship applies
to the inverse relation of books
2:57
that belong to authors.
3:02
Remember, the book model will look for
3:08
an ID that belongs to author which is
defined in the table as author_id.
3:11
Now that our relationships are defined,
let's head over to our author and
3:20
book models to add the final touches by
specifying which fields are changeable.
3:25
Protected access modifier fields,
3:31
by default are protected
from mass assignment.
3:34
But we can assign columns
that we want to change or
3:38
define as fillable such as name,
title, company and email.
3:42
It's also worth mentioning
that we wouldn't want to
3:49
use protected $fillable for
3:52
users with passwords because we wouldn't
typically want to mass assign passwords.
3:54
Instead, we would enter them individually.
4:00
We could use $hidden to hide fields from
displaying a password in the API response,
4:03
which we won't use in this course,
but it's important to keep in mind.
4:09
Next, let's head over to the author.php
file and add the name, title,
4:13
company and email, so we can specify them
as fillable inside of an array like this
4:19
Notice that I didn't include ID.
4:42
Including ID as fillable
would cause errors for
4:45
the consumer of the API because Laravel
automatically assigns an ID for us.
4:49
Finally, let's head
4:55
over to the book model and
5:00
add $fillable
5:07
fields like this.
5:11
Sweet, we can now mass assign books and
5:24
authors columns in our database
tables connected to our models.
5:27
Nice job, our Eloquent relationships
are defined as specific
5:32
column fields,
which is to say they're changeable.
5:37
To learn more about mass assignment,
protected $fillable, and
5:43
how to avoid duplicate unique IDs,
check the teacher’s notes below.
5:47
In the next section, we'll dive into
factories and seeders to quickly add
5:52
test data for application using
the powerful Faker library.
5:57
See you there.
6:02
You need to sign up for Treehouse in order to download course files.
Sign up