我想在 $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模板网!
在 Laravel 5 中创建编辑模式Creating an edit modal in Laravel 5(在 Laravel 5 中创建编辑模式)
用于集合的 Laravel 5.5 API 资源(独立数据)Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 资源(独立数据))
在 php Laravel 5 中创建自定义辅助函数的最佳实践What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么
没有“Access-Control-Allow-Origin"标头 - LaravelNo #39;Access-Control-Allow-Origin#39; header - Laravel(没有“Access-Control-Allow-Origin标头 - Laravel)
Laravel Passport Route 重定向到登录页面Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登录页面)
如何在 Laravel 5 中创建包?How create package in Laravel 5?(如何在 Laravel 5 中创建包?)