I am writing because I have spent hours of testing looking for a solution to a problem and I still have not found how to solve it. I am using Spatie Laravel Permission and it happens that I have 3 resources, one for user
, another for role
and another for permissions
. I show the structure of the mentioned resources:
UserResource.php
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'roles' => RoleResource::collection($this->whenLoaded('roles')),
'permissions' => PermissionResource::collection($this->whenLoaded('permissions', $this->getAllPermissions())),
'active' => $this->active,
];
}
}
RoleResource.php
class RoleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'permissions' => PermissionResource::collection($this->whenLoaded('permissions'))
];
}
}
PermissionResource.php
class PermissionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name
];
}
}
As you can see in my UserResource.php
file, I am defining a structure to display user roles and permissions only when required. You can also see that in the permissions
key, I am not returning only the permissions associated with the user, I am returning all the permissions directly associated with the user and associated with the roles they have getAllPermissions()
.
Now look at the RoleResource.php
file, I am defining that the permissions associated with the role will be displayed only when necessary, and here is my problem. When I do the following:
public function index()
{
return UserResource::make(User::with(['roles', 'permissions'])->find(1));
}
I am getting the following response:
Look at the key: roles
, It is showing me the permissions
relation, and that is not the expected result, I expect it to show me that result only when I do the following:
public function index()
{
return UserResource::make(User::with(['roles.permissions', 'permissions'])->find(1));
}
I've been doing a lot of testing and everything seems to indicate that when I call the method getAllPermissions()
also load the permissions
relation and that is why this is happening. Please could you help me with this problem. Thank you very much in advance.