I found it
'permissions' => $role['permissions']->map(function($permission) {
return $permission->title;
})
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this query and want to hide all the fields except title (I don't wan to use in the model protected hidden.....)
$roles = Role::with('permissions')
->paginate()
->through(fn($role) => [
'id' => $role->id.
'title' => $role->title,
'permissions' => $role['permissions']->makeVisible(['title']) // <== here
]);
I know about makeHidden ..... but I have to declare all the fields.
I want to hide all except title.
How can I do that in laravel 8 ?
Either select only the columns you want/need:
Role::with('permissions:title,role_id')
->paginate()
->through(fn($role) => [
'id' => $role->id.
'title' => $role->title,
'permissions' => $role['permissions']
]);
or, map over the Collection of permissions
Role::with('permissions')
->paginate()
->through(fn($role) => [
'id' => $role->id.
'title' => $role->title,
'permissions' => $role['permissions']->map->title
]);
Please or to participate in this conversation.