Question

I am trying to get Eager-Loading nested relationships, with constraints, to work. Everyone seems to be giving the same example of eager-loading nested relationships:

$users = User::with('posts.comments')->get();

What I want to do instead is get all the the Users related to a post of a given id. But at the same time, I also want to get the comments associated with that post.

In 4.1, I to achieve the latter, I could do:

$comments = Comment::whereHas('post', function($query) { $query->whereId(1); })->get();

Is there a way to marry this two and constrain a nested relationship?

Was it helpful?

Solution

Actually found it a lot simpler than thought.

Given these models:

class User extends \Eloquent {

    public function posts()
    {
        return $this->hasMany('Post');
    }

    public function comments()
    {
        return $this->hasManyThrough('Comment', 'Post');
    }       

}

class Post extends \Eloquent {

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function comments()
    {
        return $this->hasMany('Comment');
    }       

}

class Comment extends \Eloquent {

    public function post()
    {
        return $this->belongsTo('Post');
    }

}

We can then fetch a user that has a given post like so:

$userId = 2; $postId = 5;
$user =  User::with(['comments' => function($q) use($postId) { $q->where('post_id', $postId); }])->find($userId);

This won't work when using get() as it DOES NOT constrain the users to only those related to the given post. But this is OK since we are only interested in a single post of post_id $postId. Since a post belongsTo one and only one user, we needn't worry about the other users. We could therefore use find() or first() to get the actual results.

Also, the 'middle' relationship i.e. 'post' is automatically returned and we can get it by doing the following without having to use an extra 'with' as suggested in previous answer:

var_dump($user->posts);

OTHER TIPS

If the relations in the models are correct you can simply do:

$user = User::with('posts.comments')->get();

You can add as many relations as you want ( or up to the memory limit ;)).

It also works with pivot tables.

It's hard to achieve that in the context of User->Post->Comment, but you can easily do this starting from Post if that suit you:

Post::with('users')->with('comments')->find(1);

as it will load all users and all comments related to the given post.

-- edit: Don't trust the first sentence, it's as simple as this:

// $id of the post you want

User::with(['comments' => function ($q) { // this is hasManyThrough relation
  $q->where('post_id',$id);               // comments only related to given post
}])->with(['posts' => function ($q) {
  $q->whereId($id);                       // here we load only the post you want
}])->whereHas('posts', function ($q) {
  $q->whereId(1);                         // here we filter users by the post
})->get();

Mind though that Laravel will run 3 queries to get it done

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top