返回 api 资源时出现问题。我正在使用Spatie Laravel 权限。
碰巧在我的特殊情况下,我试图返回一个包含用户详细信息的 api 资源,以及用户拥有的角色和权限。
我的UserResorce.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')),
];
}
}
我的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
];
}
}
正如您在文件中看到的那样,我只在真正需要它们UserResource.php
时才加载roles
and 。permissions
这样,如果我执行以下操作:
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return UserResource::make(User::find(1));
}
我得到以下结果:
{
"data": {
"id":1,
"name":"User Name 1",
"email":"admin@example.com",
}
}
因此,要加载相同的结果,但这次使用与用户关联的角色,我执行以下操作:
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return UserResource::make(User::with(['roles'])->find(1));
}
正如预期的那样,我得到以下结果:
{
"data": {
"id":1,
"name":"User Name 1",
"email":"admin@example.com",
"roles": {
0: {
"id": 1,
"name": "Administrator",
}
}
}
}
现在我的问题如下,为了获取用户角色和权限,我执行了以下操作:
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return UserResource::make(User::with(['roles', 'permissions'])->find(1));
}
但是我在键中得到了一个空对象"permissions"
,所以我更仔细地分析了,是用户拥有的权限是由他们定义的role
,我查看了文档,我看到我可以使用getAllPermissions()
方法获得用户的所有权限。所以我做了以下事情:
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', function () {
return $this->getAllPermissions();
})),
];
}
}
但是通过这种方式,它也加载了permissions
角色关系,它会得到一个意想不到的结果,如下所示:
{
"data": {
"id":1,
"name":"User Name 1",
"email":"admin@example.com",
"roles": {
0: {
"id": 1,
"name": "Administrator",
"permissions": {
0: {
"id": 1
"name": "Create Users"
}
}
}
},
"permissions": {
0: {
"id": 1,
"name": "Create Users",
}
}
}
这不是预期的结果,因为我在执行时期望的结果:
return UserResource::make(User::with(['roles', 'permissions'])->find(1));
应通过以下方式:
{
"data": {
"id":1,
"name":"User Name 1",
"email":"admin@example.com",
"roles": {
0: {
"id": 1,
"name": "Administrator",
}
},
"permissions": {
0: {
"id": 1,
"name": "Create Users",
}
}
}
并在执行时:
return UserResource::make(User::with(['roles.permissions', 'permissions'])->find(1));
我期待以下结果:
{
"data": {
"id":1,
"name":"User Name 1",
"email":"admin@example.com",
"roles": {
0: {
"id": 1,
"name": "Administrator",
"permissions": {
0: {
"id": 1
"name": "Create Users"
}
}
}
},
"permissions": {
0: {
"id": 1,
"name": "Create Users",
}
}
}
有人可以帮我解决这个问题吗?非常感谢您提前。