Laravel-在本地服务器中返回false并在生产服务器中返回true的exist()方法 [英] Laravel - exists() method returning false in local and true in production server

查看:66
本文介绍了Laravel-在本地服务器中返回false并在生产服务器中返回true的exist()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚遇到这个奇怪的问题.我正在将代码部署到测试服务器,一切都一样.

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之间是否有区别?

谢谢.

推荐答案

肯定有区别.

  1. $laptop->exists检查

  1. $laptop->exists checks the exists property on the model, which determines whether the current model has been saved to the database.

$laptop->exists()调用

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆