Eloquent Relationships Laravel: Database tables are related to one another. Eloquent manages and work with easy relationships.

It supports some different types of relationship, which are as follows:

  • One to one
  • One to many
  • Many to many
  • Has one through
  • Has many through
  • One to one (polymorphic)
  • One to many (polymorphic)

Defining Eloquent Relationships

Eloquent relationships are specified as a method on our Eloquent model classes.

Eloquent models relationships also serve as powerful query builders, defining relationships as a method, provides powerful method chaining and query capabilities.


One to One

One to One: A one to one relationships is an essential relation

A one to one relationships is an essential relation. For defining these relationships, we are going to place a phone method on the owner model.

The phone method will call the hasOne method and return its result:

The hasOne method is the name which is passed as the first argument, it is a related model. The relationship is specified; we retrieve the related record by using Eloquents dynamic properties.

Dynamic properties allow us to access relationship methods as if they were properties defined on the model:

Eloquent consider as the foreign key of the relationship, which is based on the model name.

In that case, the Phone model is automatically assumed to have a user_id foreign key.

We pass a second argument to the hasOne method.

The foreign key should have a value matching the id column of the parent.

Eloquent will consider the value of the users id column in the user_id column.

To use the relationship of a value other than id, we pass the third argument to the hasOne method defining our custom key.  

The Inverse of the Relationship

We can easily access the Phone model from our Owner.

To define a relationship on the Phone model that will let us access the Owner.

 We can define the inverse of a hasOne relationship by using the belongsTo method:

In the above example, Eloquent will try to match the user_id from the Phone model to an id on the Owner model.

Eloquent consider as the default foreign key name by the name of the relationship method and the method name with _id.

The foreign key on the Phone model is not user_id, we pass a custom key as the second argument to the belongsTo method:

For Example:

one to one

Our parent model do not use id (as primary key), the third argument to the belongsTo method by defining our parent table as a custom key: 

One To Many

One To Many

A one-to-many relationship defines relationships where a single model owns any amount of the other models. One-to-many relationships are defined by placing our Eloquent model

Eloquent will automatically decide the proper foreign key column on the Comment model.

Eloquent will assume the foreign key on the Comment model as post_id.

The relationship has been specified; we can enter the collection of the comments by entering the comments correctly.  

Eloquent provides “dynamic properties”, we can enter relationship methods as they defined as the properties on the model

We can add constraints to the comments by easily call the comments method and continuing the chain conditions onto the query.

The hasOne method, we also override the foreign and local keys by passing an additional arguments to the hasMany method.

Many to Many

Many to Many

Many-to-many relations are complicated than hasOne and hasMany relationships.

For example:-

Many users have the role of “Admin”. To specify the many-to-many relationship, the three database tables which are as follows: users, roles, and role_user.

The role_user table is derived from the alphabetical order of the related model names, and it contains the user_id and role_id columns.

Many-to-many relationships are specified by writing a method which will return the result of the belongsToMany method.

Lets define the roles method on our User model:

The relationship is specified, we access the users roles by using the roles dynamic property:

We call the roles method to continue query constraints onto the relationship

The table name of the relationship joins table, and Eloquent will join the two related model names in the alphabetical order.

 We are free to override this convention as a second argument to the belongsToMany method:

We also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method.

The foreign key name of the model is the third argument, which is defining the relationship. The fourth argument joins the foreign key name of the model.

Defining Custom Intermediate Table Models

To define a custom model for representing the intermediate table of our relationship, we call the using method when defining the relationship.

Custom many-to-many pivot model extends the Illuminate\Database\Eloquent\Relations\Pivot class, for custom polymorphic many-to-many pivot models extend the Illuminate\Database\Eloquent\Relations\MorphPivot class. 

Example: – we define a Role that uses a custom RoleUser pivot model:

To define the RoleUser model, we extend the Pivot class:

We combine using with Pivot to retrieve columns from the intermediate table.

For example:-

 We retrieve the created_by and update_by columns by the RoleUser pivot table by passing the column names to the withPivot method:


Pivot models do not use the SoftDeletes. If we need to delete pivot records consider as converting our pivot model to an actual Eloquent model.

Has One Through

The “has-one-through” relationship model links across a single intermediate relation.

For Example:- 

Each supplier has one user, and each user is associated with one user record, then the supplier model access the users history through the user.

The database tables necessary, which defines this relationship:

The history table does not contain supplier_id columns; the hasOneThrough relation provides access to the owners history to the supplier model.

Lets define it on the Supplier model:

The first argument that is passed to the hasOneThrough method is the name of the final model that we will access, and the second argument is the name of the intermediate model.

Has Many Through

Has Many Through

The “has-many-through” provides a simple shortcut for accessing distant relations with an intermediate relation.

Lets look at the tables requirement for defining relationship:

The posts do not contain a country_id column; the hasManyThrough relation provides access to a countrys posts with $country->posts.

Eloquent go through the country_id to the intermediate of the users table.

After finding the matching user IDs, it will used to query the posts table.

Lets define it on the Country model:

The first argument which is passed to the hasManyThrough method is the name of the final model for access, while the second argument is the name of the intermediate model.

Polymorphic Relationships

A polymorphic relationship allows the target model that belongs to more than one type of model by 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; the target model belongs to more than one type of model on a single association.

The imageable_id column will contain the ID value of the user; the imageable_type column will contain the class name of the parent model. The imagebale_type column is used by Eloquent to determine the “type” of parent model for return when accessing the imageable relation.

Model Structure

The model definitions needed to build the relationship:

Retrieving the Relationship

Once the database table and models are defined, we can access the relationships with the models.

For example: – for retrieve the image for a post, we use the image dynamic property:

We also retrieve the parent from the polymorphic model by easily accessing the name of the method that performs the call to morphTo.

The imageable method on the Image model will access that method as a dynamic property:

The imageable relation on the Image model will return Post or User instance, it depends on which type of model owns the image. 

One to Many (Polymorphic)

A one-to-many polymorphic relation is the same as a simple one-to-many relation.

In this, the target model belongs to more than one type of model on a single association.

By using polymorphic relationships, we use a single comments table for both.

Table Structure

Model Structure

Retrieving the Relationship

Once the database table and models are defined, we access the relationships with our models.

We can also retrieve the owner from the polymorphic model by allowing the name of the method, which performs the call to morphTo.

The commentable method on the Comment model will access that method as a dynamic property:

The comment-able relation of the Comment model returns a Post or Video depends on the type of model that owns the comment. 

Pin It on Pinterest

Share This