Php 在有说服力的Laravel集合的每个元素的开头预先添加一个项目
我有这样的收藏Php 在有说服力的Laravel集合的每个元素的开头预先添加一个项目,php,laravel,collections,eloquent,laravel-5,Php,Laravel,Collections,Eloquent,Laravel 5,我有这样的收藏 [ { "objective_id": "26", "objective_weight": "50.00", "objective_description": "This is first strategic objective", "actions": [ ] }, { "objective_id": "27", "objective_weight": "12.00", "objective_descr
[
{
"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": [
]
},
]
现在我想为集合的每个元素添加一个项目。例如:
我尝试过雄辩的prepend()
方法,但没有成功
$new = $objectives->map(function($objective) {
return $objective->get()->prepend('hello', 'world');
});
return $new;
这些项目只是对象,您可以通过设置来添加属性:
$new = $objectives->map(function($objective){
$objective->foo = 'bar';
return $objective;
});
你能详细说明一下“但没有成功”吗?它是否预先准备了任何东西,是否预先准备了令人担忧的级别,或者其他什么?以下是我尝试的:$new=$objectives->map(function($objective){return$objective->get()->prepend('hello','wordl');});退回$new;你能编辑你的问题而不是添加评论吗?我这样问是因为别人更容易看到你的尝试。
$new = $objectives->map(function($objective){
$objective->foo = 'bar';
return $objective;
});