Eloquent relationships: accessing all grandchildren resources from a root model

User hasMany accounts, and Account hasMany videos.
To achieve all videos from the User model, you can define a hasManyThrough() relationship :

public function videos()
   return $this->hasManyThrough(
       'App\Video', // Videos model
       'App\Account', // Account model
       'user_id', // Foreign key on accounts table...
       'account_id', // Foreign key on videos table...
       'id', // Local key on users table...
       'id' // Local key on accounts table...

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top