Skip to content
Advertisement

Laravel Eloquent the same queries

I’ve faced this question on the interview recently.

$a = Flight::find(1); 
$b = Flight:find(1);

How many objects will be created? How many db queries will be executed? I’ll be thankful so much for any detailed explanation

Advertisement

Answer

In Either one of the code above, the query will just be 1

$a = Flight::find(1); is same as

select * from `flights` where `flights`.`id` = 1 limit 1`

Since $a & $b are 2 different variables, though they are calling the same Eloquent function, the queries are different. So the code above will result in 2 queries.

Also object created will be 2.

See here for more about Laravel Eloquent https://laravel.com/docs/8.x/eloquent

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement