Laravel Eager Loading With Parameter Example

Hello Artisan

Do you know that you can pass parameter wth eager loading. You can filter your data by passing parameter with eager loading. In this tutorial i am going to show you that eloquent pass parameters to with.

It is very simple to write this query. Just we have to pass an array with eager loading with function. Let's see the example code of laravel eager loading with parameter.

Example code : 

return User::with(['posts' => function($query){
                  $query->whereNotIn('id',[1]);
                }])
               ->get();

 

Read also : How to get month name from date in Laravel

 

In this query all the post will return without the id of 1. Hope it can help you.

 

#laravel #eloquent-tips #laravel-7x