add new element in laravel collection object

2019-02-07 19:10发布

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

what should i do, please help, thnx in advance

1条回答
Root(大扎)
2楼-- · 2019-02-07 19:15

It looks like you have everything correct according to Laravel docs, but you have a typo

$item->push($product);

Should be

$items->push($product);

I also want to think the actual method you're looking for is put

$items->put('products', $product);
查看更多
登录 后发表回答