简体   繁体   English

Laravel Eloquent 相同的查询

[英]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我将非常感谢任何详细的解释

In Either one of the code above, the query will just be 1在上述任一代码中,查询将仅为 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.由于 $a 和 $b 是 2 个不同的变量,虽然它们调用的是同一个 Eloquent 函数,但查询是不同的。 So the code above will result in 2 queries.所以上面的代码将导致 2 个查询。

Also object created will be 2.创建的对象也将为 2。

See here for more about Laravel Eloquent https://laravel.com/docs/8.x/eloquent有关 Laravel Eloquent 的更多信息,请参见此处https://laravel.com/docs/8.x/eloquent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM