lets say you have this simple collection:
$c = collect([
['active'=>false],
['active'=>false],
['active'=>false],
]
);
I want to change the last item to set active to true is there any easy collectionish way rather than something like:
$collection->toArray();
$collection[count($collection)-1]['active'] = true;
$newCollection = collect($collection);
First of all laravel collections are array accessible and you don't need to call toArray.
As for you issue you can do this:
$last = $collection->pop();
$last['active'] = true
$collection->push($last)
https://laravel.com/docs/5.2/collections#method-pop https://laravel.com/docs/5.2/collections#method-push
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With