Laravel-在本地服务器中返回false并在生产服务器中返回true的exist()方法 [英] Laravel - exists() method returning false in local and true in production server
问题描述
我刚遇到这个奇怪的问题.我正在将代码部署到测试服务器,一切都一样.
I just came across this weird problem. I was deploying my code to a test server and everything is the same.
我有一个简单的$laptop = Laptops::where('name', 'Mac')->first() ?: new BlankLaptop();
BlankLaptop
类是扩展Laptops
类的空类.
然后我就
if ($laptop->exists()) // do something
问题在于,即使我dd($laptop)
时,在生产环境中它始终返回true,结果在生产环境和本地服务器中都是 same .
The problem is that it always returns true in production, even though when I dd($laptop)
, the result is the same in both the production and the local server.
但是,当我执行$laptop->exists
(不带括号)时,它确实返回正确的值.
However, when I do $laptop->exists
(without the brackets), it does return the right value.
->exists()
和->exists
之间是否有区别?
谢谢.
推荐答案
肯定有区别.
$laptop->exists
checks theexists
property on the model, which determines whether the current model has been saved to the database.
$laptop->exists()
calls the exists
method on the query builder. It's equivalent to this:
$doesAnyLaptopExist = Laptop::query()->exists();
...运行以下SQL语句:
...which runs this SQL statement:
select exists(select * from `laptops`)
...以确定表中是否有任何条记录.
...to determine if there are any records in your table.
差异很大.
这篇关于Laravel-在本地服务器中返回false并在生产服务器中返回true的exist()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!