本文介紹了在 Laravel 集合對象中添加新元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想在 $items
數組中添加新元素,出于某些原因我不想使用連接.
I want to add new element in $items
array, I don't want to use joins for certain reasons.
$items = DB::select(DB::raw('SELECT * FROM items WHERE items.id = '.$id.' ;'));
foreach($items as $item){
$product = DB::select(DB::raw(' select * from product
where product_id = '. $id.';' ));
$item->push($product);
}
我該怎么辦?
推薦答案
看起來一切都正確 根據 Laravel 文檔,但你有一個錯字
It looks like you have everything correct according to Laravel docs, but you have a typo
$item->push($product);
應該
$items->push($product);
push
方法將一個項目追加到集合的末尾:
push
method appends an item to the end of the collection:
我還想認為您正在尋找的實際方法是 put
I also want to think the actual method you're looking for is put
$items->put('products', $product);
put
方法設置集合中給定的鍵和值
put
method sets the given key and value in the collection
這篇關于在 Laravel 集合對象中添加新元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!