Laravel Eloquent Eager Loading with Where Condition

This tutorial shows you laravel eager loading where. you'll learn laravel eager loading wherehas. i would like to show you where condition eager laravel relationship. We will use laravel eager loading with condition. You just need to some step to done laravel load relationship with condition.

Eager Loading is a concept of laravel relationship and it is a best. But we some time taking relation model data at that time we need to add condition like active data or only enabled data etc as per our logic. so we can do it in laravel eager loading.

You can see following example will easily understandable. Let's see bellow example. So might be it can help you.

Simple Condition Example:

$posts = Post::with([

'comments as active_comments' => function (Builder $query) {

$query->where('approved', 1);



Nested Condition Example:

$posts = Post::with([

'comments.user' => function (Builder $query) {

$query->where('active', 1);



I hope it can help you...

May 2, 2020 | Category : Laravel

Blog Search