Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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. :(

Last updated 1 year ago.
0

*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
Last updated 1 year ago.
0

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
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

It would still only be two queries if you use eager loading - http://laravel.com/docs/eloquent#eager-loading

Last updated 1 year ago.
0

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/Illuminat...

Last updated 1 year ago.
0

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/Illuminat...

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.