roleRepository = $roleRepository; $this->permissionRepository = $permissionRepository; } /** * Display a listing of the resource. */ public function index() { $data['title'] = "Role Lists"; $data['roles'] = $this->roleRepository->findAll(); return view('user::role.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = "Create Role"; $data['editable'] = false; $data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix(); return view('user::role.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { try { $validatedData = $request->validate([ 'name' => 'required', 'guard_name' => 'string', ]); $role = $this->roleRepository->create($validatedData); $role->permissions()->attach($request->permissions); toastr()->success('New role has been created!'); } catch (\Throwable $th) { toastr()->success($th->getMessage()); } return redirect()->route('role.index'); } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = "Edit Role"; $data['editable'] = true; $data['role'] = $this->roleRepository->getRoleById($id); $data['permissionIDsArray'] = $data['role']?->permissions?->pluck('id')->toArray(); $data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix(); return view('user::role.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id): RedirectResponse { try { $validatedData = $request->validate([ 'name' => 'required', 'guard_name' => 'string', ]); $role = $this->roleRepository->update($id, $validatedData); $role->permissions()->sync($request->permissions); toastr()->success('Role has been updated!'); } catch (\Throwable $th) { toastr()->error($th->getMessage()); } return redirect()->route('role.index'); } /** * Remove the specified resource from storage. */ public function destroy($id): Response { $this->roleRepository->delete($id); toastr()->success('Role has been deleted!'); return response()->json(['status' => true, 'message' => 'Role has been deleted!']); } }