Relation Through

  • STEP

    Models

    Country

    
    namespace App\Models;
     
     use Illuminate\Database\Eloquent\Model;
      
     class Country extends Model
     {
         public function posts()
         {
             return $this->hasManyThrough(
                 Post::class,
                 User::class,
                 'country_id', // Foreign key on users table...
                 'user_id', // Foreign key on posts table...
                 'id', // Local key on countries table...
                 'id' // Local key on users table...
             );
         }
     }
    
    

    Retrieve Records:

    
    
    $country = Country::find(1);	
     
    dd($country->posts);