Laravel Spatie权限-用户和角色关系仅获取ID和名称

时间:2020-05-30 04:29:40

标签: laravel spatie laravel-6.2

我正在使用Laravel Spatie权限来管理我的应用程序中的角色和权限。我正在训练仅在用户和角色之间的关系中检索角色ID和名称。我的UserResource看起来像这样:

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'        => $this->id,
            'name'      => $this->name,
            'surname'   => $this->surname,
            'email'     => $this->email,
            'inactive'  => $this->inactive,
            'roles'     => $this->roles
        ];
    }
}

而json看起来像这样:

"data": [
        {
            "id": 1,
            "name": "Admin",
            "surname": "Sudo",
            "email": "admin@sudo.com",
            "inactive": 0,
            "roles": [
                {
                    "id": 1,
                    "name": "Admin",
                    "guard_name": "web",
                    "created_at": "2020-05-28 23:18:58",
                    "updated_at": "2020-05-28 23:18:58",
                    "pivot": {
                        "model_id": 1,
                        "role_id": 1,
                        "model_type": "App\\User"
                    }
                }
            ]
        },
]

我真正需要的是这样的json:

"data": [
        {
            "id": 1,
            "name": "Admin",
            "surname": "Sudo",
            "email": "admin@sudo.com",
            "inactive": 0,
            "roles": [
                {
                    "id": 1,
                    "name": "Admin",
                },
                {
                    "id": 2,
                    "name": "Default",
                },
                {
                    "id": 3,
                    "name": "Guest",
                }
            ]
        },
]

我真的不知道如何修改我的模型关系或$ this-> roles参数(在UserResources中)以获取此json。

2 个答案:

答案 0 :(得分:3)

您可以创建RoleResource:

class RoleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}

并在您的UserResource上使用它:

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'        => $this->id,
            'name'      => $this->name,
            'surname'   => $this->surname,
            'email'     => $this->email,
            'inactive'  => $this->inactive,
            'roles'     => RoleResource::collection($this->roles),
        ];
    }
}

答案 1 :(得分:2)

最简单的解决方案是使用地图功能:

'roles'     => $this->roles->map(function($role){
            return [
                'id' => $role['id'],
                'name' => $role['name']
            ];
        })