本文介紹了在 Laravel 集合對(duì)象中添加新元素的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我想在 $items
數(shù)組中添加新元素,出于某些原因我不想使用連接.
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);
}
我該怎么辦?
推薦答案
看起來(lái)一切都正確 根據(jù) Laravel 文檔,但你有一個(gè)錯(cuò)字
It looks like you have everything correct according to Laravel docs, but you have a typo
$item->push($product);
應(yīng)該
$items->push($product);
push
方法將一個(gè)項(xiàng)目追加到集合的末尾:
push
method appends an item to the end of the collection:
我還想認(rèn)為您正在尋找的實(shí)際方法是 put
I also want to think the actual method you're looking for is put
$items->put('products', $product);
put
方法設(shè)置集合中給定的鍵和值
put
method sets the given key and value in the collection
這篇關(guān)于在 Laravel 集合對(duì)象中添加新元素的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!