1
votes

I've been using Eloquent for just a little while now and I can't figure out how to accomplish this. Say I have the usual Posts, Users, and Comments relationships and I want to return a list of all posts and within that list also indicate which ones the logged in user has commented on.

By querying with whereHas() or with()I've been able to return only the posts with the user's comments, or all posts with all users' comments. But how would I get all posts with only the auth user's comments? Or even just a Boolean indicating the presence of a user comment.

I feel like this should be pretty straightforward, I just haven't been able to dig up an answer in the docs or on these boards.

1

1 Answers

0
votes

Maybe something like this:

$posts = Post::whereHas('comments',function($commentQuery){
            $commentQuery->whereHas('user',function($userQuery){
                $userQuery->where('id',Auth::user()->id);
            });
        })->get();