我遇到了一個很奇怪的問題,我找不到它的原因。我想知道是否有人遇到過同樣的問題,或者是配置問題。基本上這是我的代碼塊。很簡單的東西。$productImages = App\ProductImage::where('product_id', $product->id)->orderBy("order", "asc");$productImages_First = $productImages->first();$productImages_All = $productImages->get();如果我執行轉儲并死掉,$productImages_All那么它會輸出以下內容:Collection { #items: array:5}這幾個月來一直很好。我根本沒有接觸過這塊代碼。但是,我在上周注意到,如果我$productImages_All現在轉儲并死掉,它只會返回第一個圖像而不是所有圖像。Collection { #items: array:1}似乎$productImages_First = $productImages->first();正在覆蓋它。這是標準行為嗎?如果它是對原始雄辯查詢的調用,那將是完全有意義的,我應該創建一個單獨的查詢來獲取所有圖像。然而,沒有意義的是,這段代碼已經運行了幾個月沒有改變,并且隨機損壞了。我可以很快解決這個問題,我只是想看看我是否可以更清楚地了解可能導致這種情況的原因?
1 回答
紅顏莎娜
TA貢獻1842條經驗 獲得超13個贊
這是因為當您調用第一個方法時,它會修改Eloquent/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();
- 1 回答
- 0 關注
- 204 瀏覽
添加回答
舉報
0/150
提交
取消
