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 :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the demo record associated with the user.
*/
public function Demo()
{
return $this->hasOne('App\Demo');
}
}

  • 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.
  • return $this->hasOne(‘App\Demo, ‘foreign_key’,’local_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.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
/**
* Get the user that owns the phone.
*/
public function demo()
{
return $this->belongsTo('App\Demo');
}
}

One to Many:-
One to Many relationships is defined in a single model to use relationships with other model’s data.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Demo extends Model
{
/**
* Get the comments for the blog post.
*/
public function Test()
{
return $this->hasMany('App\Exam');
}
}

  • 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.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
/**
* The roles that belong to the user.
*/
public function exam()
{
return $this->belongsToMany('App\Exam');
}
}

  • After defining the relationship you can access student’s exams using a dynamic property exam.
  • You can utilize chaining query constraints on this relationship.
  • You can simply call belongsToMany() on your related model when the inverse of Many to Many relationships is needed.
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Exam extends Model
    {
    /**
    * The users that belong to the role.
    */
    public function student()
    {
    return $this->belongsToMany('App\Student');
    }
    }
  • 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.
  • Now, we can define Has Many Through a relationship in the Demo model.
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Demo extends Model
    {
    /**
    * Get all of the tests for the country.
    */
    public function test()
    {
    return $this->hasManyThrough('App\Exam', 'App\Teacher');
    }
    }
  • 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 model structure is to build a relationship in Polymorphic relationship.
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Teacher extends Model
    {
    /**
    * Get all of the owning student models.
    */
    public function student()
    {
    return $this->morphto();
    }
    }
    class Exam extends Model
    {
    /**
    * Get all of the teachers.
    */
    public function Teacher()
    {
    return $this->morphMany('App\Techer', 'student');
    }
    }
    class Test extends Model
    {
    /**
    * Get all of the teachers.
    */
    public function Teacher()
    {
    return $this->morphMany('App\Techer', 'student');
    }
    }
  • 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.
  • The model structure of this relationship is based on the Eloquent class.
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Teacher extends Model
    {
    /**
    * Get all of the tags for the post.
    */
    public function exams()
    {
    return $this->morphToMany('App\exam', 'taggable');
    }
    }
  • After that inverse of the relationship of exams model. So you can define its related models and define teacher and test method in the below example:
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Exams extends Model
    {
    /**
    * Get all of the teachers that is assigned this teacher.
    */
    public function teacher()
    {
    return $this->morphedByMany('App\Teacher', 'taggable');
    }
    /**
    * Get all of the tests that is assigned to this teacher.
    */
    public function test()
    {
    return $this->morphedByMany('App\Test', 'taggable');
    }
    }