Laravel advanced Eloquent | eager loading

Safaetul Ahasan
3 min readJan 21, 2023

According to this relationship:

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

class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}

public function comments()
{
return $this->hasMany(Comment::class);
}
}

class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}

1. Eager loading can improve performance by reducing the number of database queries needed to load related models. For example, if you have a Post the model that has many Comment models, you can use the with method to load the comments for all posts in a single query:

$posts = Post::with('comments')->get();

2. You can use the load method to avoid unnecessary queries when eager loading. For example, if you have already loaded the comments for a post, you can use the load method to avoid loading them again:

(It is also possible to eagerly load related models directly from an already existing model collection. This may be useful when dynamically deciding whether to load related models or not, or in combination with caching.)

$post = Post::find(1);
$post->load('comments');

You can also use the load method to load multiple relationships.

$post = Post::find(1);
$post->load(['comments','likes']);

You can also use the load method to load related models for a collection of models.

$posts = Post::all();
$posts->load('comments');

This will load the comments for all posts in the collection that haven’t been loaded yet.

It’s important to keep in mind that the load method will only run the query if the relationship hasn't been loaded yet, so it can be useful for avoiding unnecessary queries and improving the performance of your application.

3. You can use nested eager loading to load multiple levels of related models in a single query. For example, if you have a Comment the model that has a User model, you can use the nested eager loading to load both the comments and the users in a single query:

$posts = Post::with(['comments' => function ($query) {
$query->with('user');
}])->get();

4. You can use the whereHas and orWhereHas method to filter parent models based on the existence of related models.

$users = User::whereHas('posts')->orWhereHas('comments')->get();

This will return all users who have at least one post or at least one comment. You can also use whereHas the method with closure like this

$users = User::whereHas('posts', function ($query) {
$query->where('title', 'like', '%example%');
})->get();

This will return all users that have at least one post with the title containing the word “example”.

5. You can use the eagerLoad method to specify the relationships to be loaded along with the main model. For example, you have a Post model that has many Comment and Like models. You can use the eagerLoad method to load only the Comment models and not the Like models like this:

$posts = Post::eagerLoad('comments')->get();

6. You can use the withCount method to retrieve the number of related models without actually loading them. For example:

$posts = Post::withCount('comments')->get();

This will load all the posts along with the number of comments for each post.

7. You can use the select method to select only the necessary columns from the related models. For example:

$posts = Post::with(['comments' => function ($query) {
$query->select(['id', 'content']);
}])->get();

This will load only the id and content columns of the comments for each post.

8. You can use the loadMissing method to load missing relationships for a collection of models. For example:

$posts = Post::all();
$posts->loadMissing('comments');

This will load the comments for all posts in the collection that were not loaded with the comments originally.

9. protected $with['model_name'] is a property that can be used to specify the relationships that should be automatically eager loaded with a model when it is retrieved from the database. For example, if you have a Post model that has many Comment models, you can specify that the comments should be automatically eager loaded with the post model by adding the following property to the Post model:

class Post extends Model
{
protected $with = ['comments'];
}

It’s important to keep in mind that when using eager loading, it could cause a lot of data to be loaded if not used properly, and it could affect the performance of your application.

--

--