php-在雄辩的Laravel集合的每个元素的开头添加一个项目
作者:互联网
我有这样的收藏
[
{
"objective_id": "26",
"objective_weight": "50.00",
"objective_description": "This is first strategic objective",
"actions": [
]
},
{
"objective_id": "27",
"objective_weight": "12.00",
"objective_description": "This second strategic objective",
"actions": [
]
},
]
现在,我想为集合的每个元素添加一个项目.例如:
[
{
"foo" : "bar"
"objective_id": "26",
"objective_weight": "50.00",
"objective_description": "This is first strategic objective",
"actions": [
...
.....
我已经尝试了Eloquent的prepend()方法,但是没有成功.
$new = $objectives->map(function($objective) {
return $objective->get()->prepend('hello', 'world');
});
return $new;
解决方法:
这些项目只是对象,您可以通过设置属性来添加属性:
$new = $objectives->map(function($objective){
$objective->foo = 'bar';
return $objective;
});
标签:laravel,laravel-5,collections,eloquent,php 来源: https://codeday.me/bug/20191120/2044189.html