Because Eloquent relationship is simple.
Also this kind of questions had been repeated. Check old forum, also GitHub if wanted. You are not first person asked this.
And if you want to use join, simply use join as you mentioned. Laravel give us free.
Thanks, quick search didnt show me what I wanted so posted the question by myself.
Eloquent relationships in models are cool, but the queries produced.. not optimized at all. :(
*edit: I stand corrected. Thanks to jarektkaczyk and others for correcting my assumption. *
You're looking for eager loading. The static function ::find will return the full model object immediately without the phone relationship. You might not need the phone relationship so always including it wouldn't be as performant in some cases.
Your original one-liner is a shortcut for:
$user = User::find(1); // select * from users where id = 1
$phone = $user->phone; // select * from phones where user_id = 1
To get the join, write it this way (untested):
$phone = User::with('phone')->where('id',1)->first()->phone; // join query
// or
$user = User::with('phone')->where('id',1)->first(); // join query
$phone = $user->phone; // no query
Eager loading won't use join and those queries result in the same queries as those of OP.
To join you need to use join() method which is as easy and eloquent as Eloquent in general ;)
Btw ::find is not a static method but Laravel Facades magic
puckbag said:
You're looking for eager loading. The static function ::find will return the full model object immediately without the phone relationship. You might not need the phone relationship so always including it wouldn't be as performant in some cases.
Your original one-liner is a shortcut for:
$user = User::find(1); // select * from users where id = 1 $phone = $user->phone; // select * from phones where user_id = 1
To get the join, write it this way (untested):
$phone = User::with('phone')->where('id',1)->first()->phone; // join query // or $user = User::with('phone')->where('id',1)->first(); // join query $phone = $user->phone; // no query
Using two queries simplifies the initialization of eloquent models. If you use join queries instead, there's a chance that two columns have the same name, for example, the ID. So, you would explicitly select the columns like so:
select `users`.`id` as user_id, `roles`.`id` as role_id from `users` inner join `roles` on `users`.`role_id` = `roles`.`id` where `users`.`id` = '1' limit 1
At the PHP side, you would then unpack the values appropriately so they can be delegated to the correct eloquent class. This would further increase the execution time and increase memory usage not to mention code complexity.
HI
SO, what would be the solution if I have a datagrid listing 100 users with their phones...?
I will surely not repeat 100 queries to get the phones for each user.
Doing a default basic SQl query I would use a join, but I dont want to code any SQL directly, or else why using Laravel.
What is hte clean and perfomring Laravel's way?
thanks by advance
It would still only be two queries if you use eager loading - http://laravel.com/docs/eloquent#eager-loading
jarektkaczyk said:
Eager loading won't use join and those queries result in the same queries as those of OP.
To join you need to use join() method which is as easy and eloquent as Eloquent in general ;)
Btw ::find is not a static method but Laravel Facades magic
puckbag said:
You're looking for eager loading. The static function ::find will return the full model object immediately without the phone relationship. You might not need the phone relationship so always including it wouldn't be as performant in some cases.
Your original one-liner is a shortcut for:
$user = User::find(1); // select * from users where id = 1 $phone = $user->phone; // select * from phones where user_id = 1
To get the join, write it this way (untested):
$phone = User::with('phone')->where('id',1)->first()->phone; // join query // or $user = User::with('phone')->where('id',1)->first(); // join query $phone = $user->phone; // no query
This isn't true, Models are not facades, find IS a static method that proxies off to the query builder. See here, https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L572
Yep, my mistake on that static!
ccovey said:
This isn't true, Models are not facades, find IS a static method that proxies off to the query builder. See here, https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L572
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community