1
I’m trying to overwrite my Collection so she can receive herself with the keys reordered, the reason is that I’m giving a Forget() and sometimes the deleted element has a key value equal to 0 (zero), and my return array ends up starting with the value 1 (a), with the example below gets better to understand:
// Just for demonstration
$collection = collect([
0 => ['nome' => 'Joao', 'idade' => 5],
1 => ['nome' => 'Gabriel', 'idade' => 6]
]);
$collection->forget(0);
$collection = $collection->values();
return $collection->toArray();
/* Resultado retornado:
[
1 => ['nome' => 'Gabriel', 'idade' => 6]
]
*/
/* Resultado esperado:
[
0 => ['nome' => 'Gabriel', 'idade' => 6]
]
*/
But if I replace the return variable name, it returns the result correctly:
// Just for demonstration
$collection = collect([
0 => ['nome' => 'Joao', 'idade' => 5],
1 => ['nome' => 'Gabriel', 'idade' => 6]
]);
$collection->forget(0);
$values = $collection->values();
return $values->toArray();
/* Resultado retornado:
[
0 => ['nome' => 'Gabriel', 'idade' => 6]
]
*/
I don’t quite understand?
– novic
I want to replace the value of $Collection with the reordered ($Collection->values()), and Laravel is only leaving if I save this Collection with another variable name, I imagine it may be the version of Laravel.
– Gabriel Becher