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

Ligonsker's avatar

Iterating joined data by unique id from main table

It's similar to the other post, but a different approach.

If I have 2 joins:

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->leftJoin('friends', 'users.id', '=', 'friends.user_id')
            ->get();

And I want to iterate by each unique user id, and display the list of his posts and friends, how can I do that?

foreach (unique user as $user) {
   // print_r(all of this $user's posts)
   // print_r(all of this $user's friends)
}
0 likes
9 replies
tykus's avatar

Using Eloquent relations would be so much easier;

$users = User::with('posts', 'friends')->get();

foreach ($users as $user) {
    dump($user->posts);
    dump($user->friends);
}
1 like
Ligonsker's avatar

@tykus I know, that's why my other post asking how to do it with Query Builder (then realized it's probably not possible in the way I thought)

The problem is that in my case, it's not simply DB::table('users'), there was a big query just on that table before I did join and I don't want to break it

So it's something like DB::table('users')->where(..)->where(..)->orderBy(..).... and on that I do the join.

Maybe I can simply change the DB::table('users') part with User::where(..)->where(..)->orderBy(..).... And keep it as it is?

And if it won't work - is there a way to do it without relationships?

*Edit: also, the joins themselves might be more complex:

        ->join('users', function ($join) {
            $join->on('users.id', '=', 'posts.user_id')->orOn(/* ... */);
        })
tykus's avatar

@Ligonsker you can use the join approach; but will need to manipulate the data afterwards to (i) group the records by the User ID; and (ii) partition those groups by some properties of post and friend.

I still think you should be able to use Eloquent; it is all just SQL at the end of the day and there are plenty of Builder methods available to you allowing constraints be applied to the query.

1 like
Snapey's avatar

learn and use eloquent. Nested data is so much easier to deal with.

1 like
Ligonsker's avatar

@Snapey I am working on existing code and it would be so annoying to change what they did there, usually I would choose eloquent for that (as you see it would've been perfect in this case) - that's why I also tried to mimic this behavior with my queries (it's pretty close, it's actually similar to what they show in the docs in eager loading)

Snapey's avatar

@Ligonsker Whats stopping you using models and adding relationships?

  • User hasMany Post
  • User hasMany Friend
1 like
Ligonsker's avatar

@Snapey In fact I started by adding the relationships, then I went to the code itself and saw that they used scattered query builder statements with many if/else conditions instead of Eloquent and I just didn't want to go deep inside that (yet), because it's working, and I needed to fix something else. I do think that if my fix will work I can then try to convert it to Eloquent

It could be as easy as just replacing all DB::table() statements in Model:: statements but the way it is written I just thought "not now" :D

Snapey's avatar

@Ligonsker you don't have to change any other code, just use it for this one query to begin with

Please or to participate in this conversation.