Skip to main content

Eloquent Relationships

Saurabh Dhariwal

Saurabh Dhariwal

Understand Laravel Eloquent Relationships

Laravel relationship tables are often connected to one another in the database. 
Laravel support different types of relationships and it’s easily working and managing data.
Hire Laravel Developer because Laravel provides powerful query builder for defined relationship for Database tables and relationship Eloquent model provides capabilities of querying and powerful chaining method.

Defining Relationships:-

  • One to One
  • One to Many
  • Many to Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

One to One:-
One to One is a basic relation in the relationship of database tables. In this relationship hasOne() method call and return result value :

Understand Laravel Eloquent Relationships
  • In hasOne method first argument passed is the name of the related model. Once a relationship is defined, we use Eloquent dynamic properties for retrieve related records.
  • Relationship model automatically assumes foreign key to id. if we wish to override or pass the third argument to specify your custom key.

    return $this->hasOne('App\Demo, 'foreign_key', 'local_key');

  • Now, we can access Demo model to our Test model. We can define belongsTo() method in Test model for access relationship Demo model.
Understand Laravel Eloquent Relationships

One to Many:-
One to Many relationships is defined in a single model to use relationships other models data.

Understand Laravel Eloquent Relationships
  • This relationship defined to hasMany() method.
  • In this relationship, we can create a relationship in a single model same as One to One inverse model. After defining relationship we can retrieve demo model for the exam.  

Many to Many:-

  • Many to Many relations are a little complex to hasOne and hasMany relationships.
  • This relationship is defined by ‘belongsToMany’ method.
Understand Laravel Eloquent Relationships
  • After defining relationship you can access student’s exams using dynamic property exam.
  • You can use chaining query constraints on this relationship.
  • You can simply call belongsToMany() on your related model when inverse of Many to Many relationships.
Understand Laravel Eloquent Relationships
  • In this relationship you can see both models counterpart are same because we are reusing the belongsToMany() on this models 

Has Many Through:-

  • Many convenient shortcuts are used to access distance relations in this relationship and all convenient shortcuts are provided by Has Many Through relationship.
  • Now, we can define Has Many Through relationship in Demo model.
Understand Laravel Eloquent Relationships
  • In above example first argument and second argument pass in hasManyThrough() method is the name of the final method and intermediate model name.

Polymorphic Relations:- 

  • Single associated model is allowed to belong more than one another model in Polymorphic relationship.
  • This relation provides a custom Polymorphic relation 
Understand Laravel Eloquent Relationships
  • You can register morphMap() in boot function in AppServiceProvider or Create a separate service. 
  • The model structure is Build a relationship in Polymorphic relationship.
Understand Laravel Eloquent Relationships
  • The polymorphic relation is a great deal to save time and share particular features. 

Many To Many Polymorphic Relations:-

  • Many To Many Polymorphic Relation Is defined in traditional polymorphic relations. For example, teacher and exam model is shared a polymorphic relation with a demo model.
  • It can allow you to unique tags in a single list and that shared in teacher and exam models.
  • Model structure of this relationship is based on Eloquent class.
Understand Laravel Eloquent Relationships
  • After that inverse of the relationship of exams model. So you can define its related models and define teacher and test method in below example :
Understand Laravel Eloquent Relationships