Laravel eloquent relationships

countries:
id | name
users:
id | name | country_id
articles:
id | body | user_id
addresses:
id | location | used_id
roles:
id | name
role_user:
id | role_id | user_id
comments:
id | body | commentable_id | commentable_type | user_id
  1. One to one
    1. User model contains hasOne() method. addresses table requires user_idusers table does not require address_id if User model uses hasOne().
    2. To use hasOne() in Address model, users table must have address_id field.
  2. One to many
    1. To use hasMany() inside User model, articles table must have user_id. But users table does not require article_id. Also, Article model does not require belongsTo() method. Source
    2. To use belongsTo() inside Article model, articles table must have user_id. But users table does not require article_id. Also, User model does not require hasMany() method. Source
    3. hasManyThroughusers table should contain country _id column and articles table should contain user_id column. Now the Country model must have the following method:
      public function articles() { 
           return $this->hasManyThrough('Article', 'User', 'country_id', 'user_id') ;
      }
      Where first parameter is the child (last) model name, second parameter is the intermediate model name, third parameter is the foreign key in the users table and fourth parameter is the foreign key inside comments table.
  3. Many to many:
    1. Pivot table: A third table role_user need to be created with columns user_id and role_idusers table or roles table does not require role_id or user_id respectively. Either or both of the model can use belongsToMany() method.
    2. Polymorphic relation: This situation occurs when a child table contains the contents of many parent table. To solve this problem with pivot table, you need to create pivot table as many as parent table. Instead, use the following technique:
      users table and articles table does not need comment_id. The comments table must have commentable_id and commentable_type. The Comment model must have
      public function commentable() {
          return $this->morphTo();
      }
      Here, the method name is the prefix of two columns in comments table.
      The User and Article model must have
      public function comments() {
           return $this->morphMany('Comment', 'commentable');
      }
      First parameter is the model name and second parameter is the method name of the Comment model.
      Note: If you add Model names in comments table, you should follow either one of the two ways:
      1. Add Fully Specified Name in commentable_type.
      2. In the boot() function of AppServiceProvider add the follwing code
        public function boot()
        {
            Relation::morphMap([
                'User' => 'App\User',
                'Article' => 'App\Article',
                // etc
            ]);
        }
        and import Relation as like below:
        use Illuminate\Database\Eloquent\Relations\Relation;

Labels: ,

© copyright-2020 Rejaul