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

rezuankassim's avatar

Can anyone help with this query?

I have three models:

User Image Post

and their relationship is:

User hasMany Image, Image belongsTo User, User hasMany Post, Post belongsTo User,

How can I query out the relationship using query builder until I have the data below?

user_name: name,
image: [
  {
    ... 
  },
  {
   ...
  }
],
post: [
  {
   ...
  },
  {
   ...
  }
]```
0 likes
7 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@rezuankassim so in your User model you've got this:

public function images()
{
    return $this->hasMany(Image::class);
}   

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

Then you can use:

User::with('images', 'posts')->get();

This will give you all the users with their images and posts.

rezuankassim's avatar

but using Eloquent to retrieve is really slow comparing to using Query Builder even if using eager loading, is there anyway to retrieve the relationship such as using joins? or any other ways?

Nakov's avatar

@rezuankassim how did you came up to that conclusion? You've tried it, and noticed a significant difference, or you just heard someone saying that?

There is a way to retrieve it using joins for sure :)

DB::table('users')->join('images', 'images.user_id', '=', 'users.id')->join('posts', 'posts.user_id', '=', 'users.id')->get();

And so on.. This is what Eloquent does behind the scenes as well.

rezuankassim's avatar

@nakov because I have tried using query builder which increased the retrieving data speed by a lot

DB::table('users')->join('images', 'images.user_id', '=', 'users.id')->join('posts', 'posts.user_id', '=', 'users.id')->get();

by using the code you given me I can only take a single record of images that belong to users, I cannot take a collection of images that belong to users

Nakov's avatar

@rezuankassim no, that gives you each row as a separate item in the collection :) so for each image, and each post it will give you a separate row. That's the benefit of using Eloquent. Otherwise you will have to do it yourself.

Eloquent is ORM, which you say it is slow, so if you can improve it by manually creating the collection then do it.

You can test what I am saying by getting images and posts for a single user using:

DB::table('users')
    ->where('users.id', 1)
    ->join('images', 'images.user_id', '=', 'users.id')
    ->join('posts', 'posts.user_id', '=', 'users.id')
    ->get();
1 like
rezuankassim's avatar

Thank you very much, personally I preferred Eloquent more than Query builder due to its simplicity, but what you can do when you working on a project which involves a few programmers with different opinion 😅 Will nested api resource make the process of retrieving data slow? Because my api resource seems to have a lot of nested resources and some logic in the resources too!

Nakov's avatar

@rezuankassim yes, everything you do on server side will be slow :)

It depends how many iterations/transformations you do on the data. That's why Eloquent can be slow as well, it depends what and how you want the data to get back to you.

Please or to participate in this conversation.