Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.8雄辩不';尽管来自它的原始转储查询可以工作,但它不能按预期工作_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel 5.8雄辩不';尽管来自它的原始转储查询可以工作,但它不能按预期工作

Php Laravel 5.8雄辩不';尽管来自它的原始转储查询可以工作,但它不能按预期工作,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,与使用原始查询不同,我在雄辩地查询时遇到了一个问题,尽管当我使用toSql() 这应该返回3行,但返回40行 $equipment = Equipment::where(["class_id" => $this->classId, "class_id2" => $this->classId, "class_id3" => $this->classId]) ->orWhere(["class_id" => 0,

与使用原始查询不同,我在雄辩地查询时遇到了一个问题,尽管当我使用
toSql()

这应该返回3行,但返回40行

$equipment = Equipment::where(["class_id" => $this->classId, "class_id2" => $this->classId, "class_id3" => $this->classId])
                    ->orWhere(["class_id" => 0, "class_id2" => 0, "class_id3" => 0])
                    ->where("type_id", ">", "7")
                    ->get();
这将返回3并按预期工作

$equipment = \DB::table("equipment")
                        ->whereRaw("(`class_id` = ".$this->classId." and `class_id2` = ".$this->classId." and `class_id3` = ".$this->classId.") 
                                    or (`class_id` = ".$this->classId." or `class_id2` = ".$this->classId." or `class_id3` = ".$this->classId.") 
                                    and `type_id` > 7")
                        ->get();
确切的原始查询雄辩转储如下:

select * 
from `equipment` 
where (`class_id` = 14 and `class_id2` = 14 and `class_id3` = 14) 
   or (`class_id` = 14 or `class_id2` = 14 or `class_id3` = 14) 
  and `type_id` > 7
在Navicat中运行此命令时,它还返回3行


这里我遗漏了什么?

您应该更明确地说明您的条件组,您可以使用匿名函数作为第一个
where()
的参数,参数为
$query
(这将是querybuilder对象本身)

您当前的查询没有多大意义,因为当
条件为时,
条件将始终为真,因此我们可以删除它的第一部分,从而简化查询


您可以使用
toSql()
而不是
get()
来调试查询,后者将打印最终的SQL字符串。如果您在这里这样做,您将看到条件变成类似于
WHERE。。或和..
,而不是
WHERE(..或..)和..

我确实使用
toSql()
调试了我的查询,但是您的函数似乎给出了
语法错误,意外的“}”
或WHERE()
的末尾忘记了冒号,现在应该修复。出于某种原因,它仍然没有返回正确的行。我仍然在看大约40行而不是3行…好吧,让我们后退一步。您能用您拥有的数据创建一个DB FIDLE(例如at)吗?您希望返回哪些行?我的猜测是,
orWhere()
部分将触发37行,比预期的多,但是没有看到实际数据和您期望的行,很难说。好吧,那么如何获得14的类ID呢?我们还可以大大简化查询。
$classID = 14;
$equipment = Equipment::where(function($query) use ($classID) {
                                  $query->where('class_id', $classID)
                                        ->orWhere('class_id2', $classID)
                                        ->orWhere('class_id3', $classID);
                        })->where("type_id", ">", "7")
                        ->get();