在 Laravel 的 Eloquent 集合的每个元素的开头添加一个项目
Prepend an item at the beginning of each element of an Eloquent collection of 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;
});
我有一个这样的合集
[
{
"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;
});