gpt4 book ai didi

php - 在 Laravel 集合对象中添加新元素

转载 作者:IT王子 更新时间:2023-10-29 00:55:58 25 4
gpt4 key购买 nike

我想在 $items 数组中添加新元素,出于某些原因我不想使用连接。

$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);
}

我该怎么办?

最佳答案

看起来你的一切都是正确的according to Laravel docs , 但是你打错了

$item->push($product);

应该是

$items->push($product);

push 方法将一个项目附加到集合的末尾:

我还想认为您正在寻找的实际方法是 put

$items->put('products', $product);

put 方法在集合中设置给定的键和值

关于php - 在 Laravel 集合对象中添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com