Skip to content

Version 1.0

Compare
Choose a tag to compare
@znck znck released this 06 Jun 21:35

Consider a blog application. In this app, a country can have many users and a user can have many articles. So, hasManyThrough provides easy way to access articles from a country.

class Country extends Model {
    public function articles () {
        return $this->hasManyThrough(Article::class, User::class);
    }
}

If we are accessing the country of the article, then we have to use $article->user->country.

Class Article extends Model {
    public function country() {
        return $this->belongsToThrough(Country::class, User::class);
    }
}

Now, the magic: $article->country