Laravel Eloquent : Relationship Querying on relationships


Eloquent also lets you query on defined relationships, as show below:

User::whereHas('articles', function (Builder $query) {
    $query->where('published', '!=', true);

This requires that your relationship method name is articles in this case. The argument passed into the closure is the Query Builder for the related model, so you can use any queries here that you can elsewhere.

Eager Loading

Suppose User model has a relationship with Article model and you want to eager load the related articles. This means the articles of the user will be loaded while retrieving user.

articles is the relationship name (method) in User model.


if you have multiple relationship. for example articles and posts.


and to select nested relationships


Call more than one nested relationship