Eloquent: Relationships
Introduction
Database tables are often related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy, and supports several different types of relationships:
- One To One
- One To Many
- Many To Many
- Has One Through
- Has Many Through
- One To One (Polymorphic)
- One To Many (Polymorphic)
- Many To Many (Polymorphic)
Defining Relationships
Eloquent relationships are defined as methods on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities. For example, we may chain additional constraints on this posts
relationship:
$user->posts()->where('active', 1)->get();
But, before diving too deep into using relationships, let's learn how to define each type.
Relationship names cannot collide with attribute names as that could lead to your model not being able to know which one to resolve.
One To One
A one-to-one relationship is a very basic relation. For example, a User
model might be associated with one Phone
. To define this relationship, we place a phone
method on the User
model. The phone
method should call the hasOne
method and return its result:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
The first argument passed to the hasOne
method is the name of the related model. Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship methods as if they were properties defined on the model:
$phone = User::find(1)->phone;
Eloquent determines the foreign key of the relationship based on the model name. In this case, the Phone
model is automatically assumed to have a user_id
foreign key. If you wish to override this convention, you may pass a second argument to the hasOne
method:
return $this->hasOne('App\Phone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the id
(or the custom $primaryKey
) column of the parent. In other words, Eloquent will look for the value of the user's id
column in the user_id
column of the Phone
record. If you would like the relationship to use a value other than id
, you may pass a third argument to the hasOne
method specifying your custom key:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
Defining The Inverse Of The Relationship
So, we can access the Phone
model from our User
. Now, let's define a relationship on the Phone
model that will let us access the User
that owns the phone. We can define the inverse of a hasOne
relationship using the belongsTo
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
In the example above, Eloquent will try to match the user_id
from the Phone
model to an id
on the User
model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id
. However, if the foreign key on the Phone
model is not user_id
, you may pass a custom key name as the second argument to the belongsTo
method:
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key');
}
If your parent model does not use id
as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the belongsTo
method specifying your parent table's custom key:
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
One To Many
A one-to-many relationship is used to define relationships where a single model owns any amount of other models. For example, a blog post may have an infinite number of comments. Like all other Eloquent relationships, one-to-many relationships are defined by placing a function on your Eloquent model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Remember, Eloquent will automatically determine the proper foreign key column on the Comment
model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id
. So, for this example, Eloquent will assume the foreign key on the Comment
model is post_id
.
Once the relationship has been defined, we can access the collection of comments by accessing the comments
property. Remember, since Eloquent provides "dynamic properties", we can access relationship methods as if they were defined as properties on the model:
$comments = App\Post::find(1)->comments;
foreach ($comments as $comment) {
//
}
Since all relationships also serve as query builders, you can add further constraints to which comments are retrieved by calling the comments
method and continuing to chain conditions onto the query:
$comment = App\Post::find(1)->comments()->where('title', 'foo')->first();
Like the hasOne
method, you may also override the foreign and local keys by passing additional arguments to the hasMany
method:
return $this->hasMany('App\Comment', 'foreign_key');
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
One To Many (Inverse)
Now that we can access all of a post's comments, let's define a relationship to allow a comment to access its parent post. To define the inverse of a hasMany
relationship, define a relationship function on the child model which calls the belongsTo
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}
Once the relationship has been defined, we can retrieve the Post
model for a Comment
by accessing the post
"dynamic property":
$comment = App\Comment::find(1);
echo $comment->post->title;
In the example above, Eloquent will try to match the post_id
from the Comment
model to an id
on the Post
model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a _
followed by the name of the primary key column. However, if the foreign key on the Comment
model is not post_id
, you may pass a custom key name as the second argument to the belongsTo
method:
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key');
}
If your parent model does not use id
as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the belongsTo
method specifying your parent table's custom key:
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
Many To Many
Many-to-many relations are slightly more complicated than hasOne
and hasMany
relationships. An example of such a relationship is a user with many roles, where the roles are also shared by other users. For example, many users may have the role of "Admin".
Table Structure
To define this relationship, three database tables are needed: users
, roles
, and role_user
. The role_user
table is derived from the alphabetical order of the related model names, and contains the user_id
and role_id
columns:
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
Model Structure
Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany
method. For example, let's define the roles
method on our User
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
Once the relationship is defined, you may access the user's roles using the roles
dynamic property:
$user = App\User::find(1);
foreach ($user->roles as $role) {
//
}
Like all other relationship types, you may call the roles
method to continue chaining query constraints onto the relationship:
$roles = App\User::find(1)->roles()->orderBy('name')->get();
As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany
method:
return $this->belongsToMany('App\Role', 'role_user');
In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany
method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
Defining The Inverse Of The Relationship
To define the inverse of a many-to-many relationship, you place another call to belongsToMany
on your related model. To continue our user roles example, let's define the users
method on the Role
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
As you can see, the relationship is defined exactly the same as its User
counterpart, with the exception of referencing the App\User
model. Since we're reusing the belongsToMany
method, all of the usual table and key customization options are available when defining the inverse of many-to-many relationships.
Retrieving Intermediate Table Columns
As you have already learned, working with many-to-many relations requires the presence of an intermediate table. Eloquent provides some very helpful ways of interacting with this table. For example, let's assume our User
object has many Role
objects that it is related to. After accessing this relationship, we may access the intermediate table using the pivot
attribute on the models:
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
Notice that each Role
model we retrieve is automatically assigned a pivot
attribute. This attribute contains a model representing the intermediate table, and may be used like any other Eloquent model.
By default, only the model keys will be present on the pivot
object. If your pivot table contains extra attributes, you must specify them when defining the relationship:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
If you want your pivot table to have automatically maintained created_at
and updated_at
timestamps, use the withTimestamps
method on the relationship definition:
return $this->belongsToMany('App\Role')->withTimestamps();
Customizing The pivot
Attribute Name
As noted earlier, attributes from the intermediate table may be accessed on models using the pivot
attribute. However, you are free to customize the name of this attribute to better reflect its purpose within your application.
For example, if your application contains users that may subscribe to podcasts, you probably have a many-to-many relationship between users and podcasts. If this is the case, you may wish to rename your intermediate table accessor to subscription
instead of pivot
. This can be done using the as
method when defining the relationship:
return $this->belongsToMany('App\Podcast')
->as('subscription')
->withTimestamps();
Once this is done, you may access the intermediate table data using the customized name:
$users = User::with('podcasts')->get();
foreach ($users->flatMap->podcasts as $podcast) {
echo $podcast->subscription->created_at;
}
Filtering Relationships Via Intermediate Table Columns
You can also filter the results returned by belongsToMany
using the wherePivot
, wherePivotIn
, and wherePivotNotIn
methods when defining the relationship:
return $this->belongsToMany('App\Role')->wherePivot('approved', 1);
return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);
return $this->belongsToMany('App\Role')->wherePivotNotIn('priority', [1, 2]);
Defining Custom Intermediate Table Models
If you would like to define a custom model to represent the intermediate table of your relationship, you may call the using
method when defining the relationship. Custom many-to-many pivot models should extend the Illuminate\Database\Eloquent\Relations\Pivot
class while custom polymorphic many-to-many pivot models should extend the Illuminate\Database\Eloquent\Relations\MorphPivot
class. For example, we may define a Role
which uses a custom RoleUser
pivot model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User')->using('App\RoleUser');
}
}
When defining the RoleUser
model, we will extend the Pivot
class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class RoleUser extends Pivot
{
//
}
You can combine using
and withPivot
in order to retrieve columns from the intermediate table. For example, you may retrieve the created_by
and updated_by
columns from the RoleUser
pivot table by passing the column names to the withPivot
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User')
->using('App\RoleUser')
->withPivot([
'created_by',
'updated_by',
]);
}
}
Pivot models may not use the SoftDeletes
trait. If you need to soft delete pivot records consider converting your pivot model to an actual Eloquent model.
Custom Pivot Models And Incrementing IDs
If you have defined a many-to-many relationship that uses a custom pivot model, and that pivot model has an auto-incrementing primary key, you should ensure your custom pivot model class defines an incrementing
property that is set to true
.
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
Has One Through
The "has-one-through" relationship links models through a single intermediate relation.
For example, in a vehicle repair shop application, each Mechanic
may have one Car
, and each Car
may have one Owner
. While the Mechanic
and the Owner
have no direct connection, the Mechanic
can access the Owner
through the Car
itself. Let's look at the tables necessary to define this relationship:
mechanics
id - integer
name - string
cars
id - integer
model - string
mechanic_id - integer
owners
id - integer
name - string
car_id - integer
Now that we have examined the table structure for the relationship, let's define the relationship on the Mechanic
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Mechanic extends Model
{
/**
* Get the car's owner.
*/
public function carOwner()
{
return $this->hasOneThrough('App\Owner', 'App\Car');
}
}
The first argument passed to the hasOneThrough
method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.
Typical Eloquent foreign key conventions will be used when performing the relationship's queries. If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasOneThrough
method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model:
class Mechanic extends Model
{
/**
* Get the car's owner.
*/
public function carOwner()
{
return $this->hasOneThrough(
'App\Owner',
'App\Car',
'mechanic_id', // Foreign key on cars table...
'car_id', // Foreign key on owners table...
'id', // Local key on mechanics table...
'id' // Local key on cars table...
);
}
}
Has Many Through
The "has-many-through" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country
model might have many Post
models through an intermediate User
model. In this example, you could easily gather all blog posts for a given country. Let's look at the tables required to define this relationship:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
Though posts
does not contain a country_id
column, the hasManyThrough
relation provides access to a country's posts via $country->posts
. To perform this query, Eloquent inspects the country_id
on the intermediate users
table. After finding the matching user IDs, they are used to query the posts
table.
Now that we have examined the table structure for the relationship, let's define it on the Country
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
/**
* Get all of the posts for the country.
*/
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User');
}
}
The first argument passed to the hasManyThrough
method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.
Typical Eloquent foreign key conventions will be used when performing the relationship's queries. If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasManyThrough
method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model:
class Country extends Model
{
public function posts()
{
return $this->hasManyThrough(
'App\Post',
'App\User',
'country_id', // Foreign key on users table...
'user_id', // Foreign key on posts table...
'id', // Local key on countries table...
'id' // Local key on users table...
);
}
}
Polymorphic Relationships
A polymorphic relationship allows the target model to belong to more than one type of model using a single association.
One To One (Polymorphic)
Table Structure
A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association. For example, a blog Post
and a User
may share a polymorphic relation to an Image
model. Using a one-to-one polymorphic relation allows you to have a single list of unique images that are used for both blog posts and user accounts. First, let's examine the table structure:
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
Take note of the imageable_id
and imageable_type
columns on the images
table. The imageable_id
column will contain the ID value of the post or user, while the imageable_type
column will contain the class name of the parent model. The imageable_type
column is used by Eloquent to determine which "type" of parent model to return when accessing the imageable
relation.
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
/**
* Get the owning imageable model.
*/
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get the post's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
class User extends Model
{
/**
* Get the user's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
Retrieving The Relationship
Once your database table and models are defined, you may access the relationships via your models. For example, to retrieve the image for a post, we can use the image
dynamic property:
$post = App\Post::find(1);
$image = $post->image;
You may also retrieve the parent from the polymorphic model by accessing the name of the method that performs the call to morphTo
. In our case, that is the imageable
method on the Image
model. So, we will access that method as a dynamic property:
$image = App\Image::find(1);
$imageable = $image->imageable;
The imageable
relation on the Image
model will return either a Post
or User
instance, depending on which type of model owns the image. If you need to specify custom type
and id
columns for the morphTo
relation, always ensure you pass the relationship name (which should exactly match the method name) as the first parameter:
/**
* Get the model that the image belongs to.
*/
public function imageable()
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}
One To Many (Polymorphic)
Table Structure
A one-to-many polymorphic relation is similar to a simple one-to-many relation; however, the target model can belong to more than one type of model on a single association. For example, imagine users of your application can "comment" on both posts and videos. Using polymorphic relationships, you may use a single comments
table for both of these scenarios. First, let's examine the table structure required to build this relationship:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Model Structure
Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get the owning commentable model.
*/
public function commentable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
class Video extends Model
{
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Retrieving The Relationship
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the comments for a post, we can use the comments
dynamic property:
$post = App\Post::find(1);
foreach ($post->comments as $comment) {
//
}
You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo
. In our case, that is the commentable
method on the Comment
model. So, we will access that method as a dynamic property:
$comment = App\Comment::find(1);
$commentable = $comment->commentable;
The commentable
relation on the Comment
model will return either a Post
or Video
instance, depending on which type of model owns the comment.