If you enjoy reading my blog, you could consider supporting me on Patreon.

Laravel's HasManyThrough cheatsheet

- The current model Country has a relation to Post via User
- The intermediate model is linked to the current model via users.country_id
- The target model is linked to the intermediate model via posts.user_id
- users.country_id maps to countries.id
- posts.user_id maps to users.id
    id - integer
    name - string

    id - integer
    country_id - integer
    name - string

    id - integer
    user_id - integer
    title - string
class Country extends Model
    public function posts()
        return $this->hasManyThrough(
            '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...
Would you like to stay up to date about new content? Feel free to subscribe to my newsletter and follow me on Twitter. Noticed a tpyo? You can submit a PR to fix it.