Laravel 4.1 Eager Loading Nested Relationships with Constraints

后端 未结 3 1737
一整个雨季
一整个雨季 2021-02-08 03:03

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:

<         


        
相关标签:
3条回答
  • 2021-02-08 03:32

    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.

    0 讨论(0)
  • 2021-02-08 03:49

    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

    0 讨论(0)
  • 2021-02-08 03:50

    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);
    
    0 讨论(0)
提交回复
热议问题