Laravel Framework Relationships in Eloquent Class

Webner Solutions
3 min readMay 7, 2021

--

Laravel Framework Relationships in Eloquent Class
Laravel Framework Relationships in Eloquent Class

Laravel relationship tables are frequently associated with each other in the database. Laravel relationships in eloquent support different types of relationships and it’s easily working and managing data.

Defining relationships:-

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

One to one relationship
One to One is a basic relationship in the relationship of database tables. In this relationship hasOne() strategy call and return result esteem :

  • In hasOne technique first argument passed is the name of the related model. Once a relationship is defined, we use Eloquent dynamic properties to retrieve related records.
  • The relationship model automatically assumes a foreign key to id. In the event that we wish to override or pass the third argument to indicate your custom key.
  • Now, we can access the Demo model to our Test model. We can characterize belongsTo() technique in Test model for access relationship Demo model.

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

  • This relationship is defined by the ‘hasMany’ method.
  • In this relationship, we can create a relationship in a single model same as in the One to One inverse model. After defining the relationship we can retrieve the demo model for the exam.

Many to Many:-

  • Many to Many relations are a little complex as compared to hasOne and hasMany relationships.
  • This relationship is defined by the ‘belongsToMany’ method.
  • After defining the relationship you can access student’s exams using a dynamic property exam.
  • You can utilize chaining query constraints on this relationship.
  • In this relationship you can see both models counterpart are the same because we are reusing the belongsToMany() on this models

Has Many Through:-

  • Numerous advantageous — Easy shortcuts are utilized to get to separate relations in this relationship and all helpful alternate ways are given by Has Many Through relationship.
  • In the above example, the first argument and second argument pass in hasManyThrough() method is the name of the final method and intermediate model name.

Polymorphic Relations:-

  • A single associated model is allowed to belong more than one another model in a Polymorphic relationship.
  • This relation provides a custom Polymorphic relation
    <?php
    use Illuminate\Database\Eloquent\Relations\Relation;
    Relation::morphMap(['Teacher' => 'App\Teacher', 'Exam' => 'App\Exam', ])
  • You can register morphMap() in the boot function in AppServiceProvider or Create a separate service.
  • 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, the teacher and exam model has 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.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Originally published at https://blog.webnersolutions.com on May 7, 2021.

--

--

Webner Solutions
Webner Solutions

Written by Webner Solutions

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

No responses yet