Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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 为什么我的雄辩的查询在以后使用时会被覆盖?_Php_Laravel_Eloquent - Fatal编程技术网

Php 为什么我的雄辩的查询在以后使用时会被覆盖?

Php 为什么我的雄辩的查询在以后使用时会被覆盖?,php,laravel,eloquent,Php,Laravel,Eloquent,我遇到了一个非常奇怪的问题,我找不到它的原因。我想知道是否有人遇到了同样的问题,或者是配置问题 基本上这是我的代码块。非常简单的东西 $productImages = App\ProductImage::where('product_id', $product->id)->orderBy("order", "asc"); $productImages_First = $productImages->first(); $productImages_All = $productIm

我遇到了一个非常奇怪的问题,我找不到它的原因。我想知道是否有人遇到了同样的问题,或者是配置问题

基本上这是我的代码块。非常简单的东西

$productImages = App\ProductImage::where('product_id', $product->id)->orderBy("order", "asc");
$productImages_First = $productImages->first();
$productImages_All = $productImages->get();
如果我对$productImages\u All执行转储并死亡,那么它将输出以下内容:

Collection {
  #items: array:5
}
这几个月来一直运作良好。我根本没碰过这段代码

然而,我在上周注意到,如果我现在转储并死亡$productImages\u All,它将只返回第一个图像,而不是所有图像

Collection {
  #items: array:1
}
似乎$productImages\u First=$productImages->First;正在覆盖它。这是标准行为吗?这将是完全有意义的,如果它是作为它调用原始雄辩的查询,我应该创建一个单独的一个,以获得所有的图像。然而,没有意义的是,这段代码已经工作了几个月,没有任何更改,并且随机中断


我可以很快地解决这个问题,我只是想看看是否可以更清楚地了解可能导致这个问题的原因?

这是因为当您调用第一个方法时,它会修改雄辩/Builder中的Query/Builder对象,并将limit属性设置为1,所以您必须在调用get方法之前重置它的值

$productImages = App\ProductImage::where('product_id', $product->id)- 
>orderBy("order", "asc");
$productImages_First = (clone $productImages)->first();
$productImages_All = $productImages->get();
或者你可以

$productImages = App\ProductImage::where('product_id', $product->id)- 
>orderBy("order", "asc");
$productImages_First = $productImages->first();
$productImages->getQuery()->limit=null;
$productImages_All = $productImages->get();

也许你的数据有变化?对我来说很好。答案已经修改了。请检查一下。太棒了。非常感谢。我不知道有克隆人这种东西!