allowedFilters(['id', 'name']) ->allowedSorts(['id', 'name']) ->paginate($request->query('per_page') ?? 10); return $this->fractal->collection($roles) ->transformWith($this->getTransformer(RoleTransformer::class)) ->toArray(); } /** * Return a single role. */ public function view(GetRoleRequest $request, Role $role): array { return $this->fractal->item($role) ->transformWith($this->getTransformer(RoleTransformer::class)) ->toArray(); } /** * Store a new role on the Panel and return an HTTP/201 response code with the * new role attached. * * @throws \Throwable */ public function store(StoreRoleRequest $request): JsonResponse { $role = Role::create($request->validated()); return $this->fractal->item($role) ->transformWith($this->getTransformer(RoleTransformer::class)) ->addMeta([ 'resource' => route('api.application.roles.view', [ 'role' => $role->id, ]), ]) ->respond(201); } /** * Update a role on the Panel and return the updated record to the user. * * @throws \Throwable */ public function update(UpdateRoleRequest $request, Role $role): array { if ($role->isRootAdmin()) { throw new PanelException('Can\'t update root admin role!'); } $role->update($request->validated()); return $this->fractal->item($role) ->transformWith($this->getTransformer(RoleTransformer::class)) ->toArray(); } /** * Delete a role from the Panel. * * @throws \Exception */ public function delete(DeleteRoleRequest $request, Role $role): Response { if ($role->isRootAdmin()) { throw new PanelException('Can\'t delete root admin role!'); } $role->delete(); return $this->returnNoContent(); } }