'"flatten" collection "With('model')"
I've been trying to get my head around this for a while, tried flatten, map, and a bunch of other things in "https://laravel.com/docs/8.x/collections" but nothing seems to work.
Other than doing a foreach and creating a new array (which is super sub-optimal); there has to be a better way...right?
Note: I've read all the answers on similar issues, nothing works
Heres my query:
$users = User::where('active',1)
->whereHas('role', function ($query) use ($column, $role){
$query->where('role_'.$column, $role);
})
->with('role:id,role_name,role_category')
->orderBy('users.id')
->get([
'id',
'email',
'phone_number',
'id_role',
'firstname',
'lastname',
'verified',
'active',
'id_configuration',
'external_service_id',
]);
I'm getting this output:
[{
"id": 968,
"email": "[email protected]",
"phone_number": "123",
"id_role": 4,
"firstname": "Name",
"lastname": "TEST User",
"verified": 0,
"active": 1,
"id_configuration": 1,
"external_service_id": 123,
"role": {
"id": 4,
"role_name": "Admin",
"role_category": "Company"
}
}]
But the output I need to return is this:
[{
"id": 968,
"email": "[email protected]",
"phone_number": "12312",
"id_role": 4,
"firstname": "Admin",
"lastname": "TEST User",
"verified": 0,
"active": 1,
"id_configuration": 1,
"external_service_id": 123,
"role_name": "Admin",
"role_category": "Company"
}]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
