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 A
s, then all of their referenced B
s and finally all of the C
s referenced by the loaded B
s. 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
Pdo Were Rows Affected During Execute Statement
Simple PHP Echo Code Not Working
PHP How to Remove Last Part of a Path
File_Get_Contents Returns Empty String
How to Search Array for Multiple Values in PHP
Sending/Displaying a Base64 Encoded Image
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types
What Means Call to a Member Function on Boolean and How to Fix
Convert PHP Array String into an Array
Using Return in Ternary Operator
Multiple Http Get Parameters with the Same Identifier
Why Would I Use Dirname(_File_) in an Include or Include_Once Statement
PHP Code Formatter/Beautifier and PHP Beautification in General
Show Progressbar While Loading Pages Using Jquery Ajax in Single Page Website
Setting Up Domainkeys/Dkim in a PHP-Based Smtp Client
Get Root Directory Path of a PHP Project
Trying to Pass Todataurl with Over 524288 Bytes Using Input Type Text