Laravel - Collections - Method Replace
The replace
method behaves similarly to merge
; however, in addition to overwriting matching items that have string keys, the replace
method will also overwrite items in the collection that have matching numeric keys:
$collection = collect(['Taylor', 'Abigail', 'James']);
$replaced = $collection->replace([1 => 'Victoria', 3 => 'Finn']);
$replaced->all();
// ['Taylor', 'Victoria', 'James', 'Finn']