Reputation: 1177
I have two queries that return a collection. But with different ids I get different array indexes.
$worker = Worker::find($worker_id);
$man = $worker->managers->where('id', $manager_id)->first();
$tasks = $man->tasks->where('worker_id', $worker_id);
dd($tasks->toArray());
When I run this query with $worker_id
of 1, I get an array with numeric index starting from 0:
[
{
"id": 1,
"task_name": "Cleaning"
},
....
]
But with $worker_id
of 2, I get an array with named (string) indexes starting from "9":
[
"9": {
"id": 18,
"task_name": "Staff reorientation"
},
"10": {
"id": 19,
"task_name": "Schedule"
}
....
]
What may be the cause?
Upvotes: 0
Views: 688
Reputation: 25926
Filtering a collection doesn't change the original keys. You can reset them with values()
:
$tasks = $man->tasks->where('worker_id', $worker_id)->values();
Upvotes: 1