Currently using spatie/laravel-permission package to manage user roles: an user could have a single role.
return $this->userModel
->select('id', 'first_name', 'last_name', 'email', 'slug')
->with(['roles' => function ($query) {
$query->select('name', 'guard_name');
}])
->find($id);
Hoever, when I'm running the following query, it returns an array of roles.
"roles": [
{
"name": "personal_trainer",
"pivot": {
"model_id": "9e8fb1a1-e725-4b48-befa-d9c4783c9eda",
"role_id": 2,
"model_type": "App\\Models\\User\\User"
}
}
],
How can I make "roles" key return a single object instead of an array? I want to avoid manipulating it post-query.
Kindly try
return $this->userModel
->select('id', 'first_name', 'last_name', 'email', 'slug')
->with(['roles' => function ($query) {
$query->select('name', 'guard_name');
}])
->first();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.