roles->all(); $permissions = $this->permissions->all(); return Inertia::render('permissions/Index', [ 'roles' => $roles->map(fn (Role $role) => [ 'id' => $role->id, 'display_name' => $role->display_name, ])->values()->all(), 'permissions' => $permissions->map(fn (Permission $permission) => [ 'id' => $permission->id, 'name' => $permission->name, 'display_name' => $permission->display_name, 'removable' => (bool) $permission->removable, 'editUrl' => route('permissions.edit', $permission), 'deleteUrl' => route('permissions.destroy', $permission), ])->values()->all(), // Replaces the per-checkbox $role->hasPermission() calls the Blade // matrix made, so the client can render the grid from plain data. 'granted' => $roles->mapWithKeys(fn (Role $role) => [ $role->id => $permissions ->filter(fn (Permission $permission) => $role->hasPermission($permission->name)) ->pluck('id') ->values() ->all(), ])->all(), ]); } public function create(): InertiaResponse { return Inertia::render('permissions/Form', ['edit' => false, 'permission' => null]); } public function store(CreatePermissionRequest $request): RedirectResponse { $this->permissions->create($request->all()); return redirect()->route('permissions.index') ->withSuccess(__('Permission created successfully.')); } public function edit(Permission $permission): InertiaResponse { return Inertia::render('permissions/Form', [ 'edit' => true, 'permission' => [ 'id' => $permission->id, 'name' => $permission->name, 'display_name' => $permission->display_name, 'description' => $permission->description, ], ]); } public function update(Permission $permission, UpdatePermissionRequest $request): RedirectResponse { $this->permissions->update($permission->id, $request->all()); return redirect()->route('permissions.index') ->withSuccess(__('Permission updated successfully.')); } /** * @throws NotFoundHttpException */ public function destroy(Permission $permission) { if (! $permission->removable) { throw new NotFoundHttpException; } $this->permissions->delete($permission->id); return redirect()->route('permissions.index') ->withSuccess(__('Permission deleted successfully.')); } }