Prepare an item at the start of each item in the Laravel Eloquent Collection
I have a collection like this
[
{
"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": [
]
},
]
Now I want to add an item for each item in the collection. eg:
[
{
"foo" : "bar"
"objective_id": "26",
"objective_weight": "50.00",
"objective_description": "This is first strategic objective",
"actions": [
...
.....
I tried the prepend()
Eloquent method but couldn't.
$new = $objectives->map(function($objective) {
return $objective->get()->prepend('hello', 'world');
});
return $new;
+3
Nasif Md. Tanjim
source
to share
1 answer
These objects are just objects and you can add a property just by setting it:
$new = $objectives->map(function($objective){
$objective->foo = 'bar';
return $objective;
});
+3
lukasgeiter
source
to share