Laravel Eager Loading - Load Only Specific Columns

Laravel Eager Loading - Load only specific columns

Make use of the select() method:

public function car() {
return $this->hasOne('Car', 'id')->select(['owner_id', 'emailid', 'name']);
}

Note: Remember to add the columns assigned to the foreign key matching both tables. For instance, in my example, I assumed a Owner has a Car, meaning that the columns assigned to the foreign key would be something like owners.id = cars.owner_id, so I had to add owner_id to the list of selected columns;

Laravel 5.6 Eager Loading specific column returns nothing

You always need foreign key/primary key, involved in the relation, to be selected. fetch product_id too in eager load and it will work

Product::with('formats:id,upc,product_id')->get();

Laravel: Eager loading specific columns

You need to select like this way :

Order::with(['products'=>function($q){
$q->orderBy('name','asc');
}])->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')->paginate($length);

Or without sub query :

Order::with('products')
->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')
->orderBy('name','asc');
->paginate($length);

Laravel select specific columns with Eloquent while eager loading

You have to make sure you are selecting the id's and any foreign keys that would be needed for the relationship from either side of that relationship. This allows Eloquent to match up parents to their children.

Laravel nested eager load specific columns

Laravel is loading each level of relationships after another. In other words, if you use A::with('b.c')->get(), then Eloquent will first load all As, then all of their referenced Bs and finally all of the Cs referenced by the loaded Bs. The ORM uses navigation properties, i.e. foreign keys, to do so. If you omit these foreign keys on intermediate models, the framework is not able to load the referenced models anymore.

If you'd do it manually, you would use the following queries (used IDs and foreign keys are examples):

SELECT * FROM a;                         // returns As with ids 1, 2, 3
SELECT * FROM b WHERE a_id IN (1, 2, 3); // returns Bs with ids 4, 5, 6
SELECT * FROM c WHERE b_id IN (4, 5, 6);

In your case, it should be sufficient to use the following code:

public function show(string $id)
{
$film = Film::with([
'account.user:id,account_id,location_id,name',
'account.user.location:id,city'
])->findOrFail($id);
}

Laravel how to load specific column of related table?

To select only specific columns from a relationship you should be able to do it like this:

auth()->user()->load([
'business' => function ( $query ) {
$query->select('id', 'title');
},
'passwordSecurity',
]);

Or like this:

auth()->user()->load(['business:id,name', 'passwordSecurity']);

Please note that you have to select IDs and foreign key constraints that are needed to form this relationship.



Related Topics



Leave a reply



Submit