This article will provide some of the most important example of laravel eloquent like eager loading specific columns example. You will learn eager loading specific columns. you will learn with select columns laravel.
Eager Loading is a part of laravel relationship and it is a best way to show data from database. But we some time we just need to print just specific columns from relation model. We need not all the data from eager load method. Let's see get specific columns using with function in laravel eloquent.
You may not always need every column from the relationships you are retrieving. For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve:
return Post::with('user:id,name')
->get();
Or you can use this query to pass parameter with with() method.
return $posts = Post::with(['user' => function($query) {
return $query->select(['id', 'name']);
}])->get();
I hope it can help you.
#laravel #eloquent-tips #laravel-7x