Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ibzy's avatar
Level 2

How do I query three tables to send the info to the view

I am trying to do an activity function where users can monitor what their friends have been doing in terms of likes and comments on the app. I have managed to get the like activity this is how I have done it


public function activity()
    {
        $friends = Friendship::where('follower_id', Auth::id())->pluck('following_id');
        $like_activity = Like::whereIn('likes.user_id', $friends)->get();

        return view('friends.activity', compact('like_activity'));
    }

However I am stuck on passing the comment activity of the connected user's friends. I have 2 tables (likes and comments). I want to do a join to get them both and pass it to the view in one go.

0 likes
1 reply
OriOn's avatar

Hello, It sounds to me like you want to have a look at eager loading.

it's more or less what you are doing for the likes except that it's adding it to the $friends collection. Your models need to be related though.

I guess friends are users,

users have many likes,

users have many comments

you can load a user, his likes and his comments like so:

\App\User::with('comments', 'likes')->find($userId);

now the same principle applies if you are querying many users, like via your friendship relation

1 like

Please or to participate in this conversation.