userRepository = $userRepository; $this->roleRepository = $roleRepository; } public function index() { $data['users'] = $this->userRepository->findAll(); $data['editable'] = false; $data['roleLists'] = $this->roleRepository->pluck(); return view('crud.generated.users.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = "Create User"; $data['editable'] = false; $data['roleLists'] = $this->roleRepository->pluck(); return view('crud.generated.users.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { // try { $validated = $request->validate([ 'name' => 'required|min:5', 'email' => 'required', 'password' => 'required', ]); $validated['password'] = bcrypt($validated['password']); $this->userRepository->create($validated, $request->role); Toastr::success('User has been created successfully.', 'Title', ["positionClass" => "toast-top-center"]); // } catch (\Throwable $th) { // echo $th->getMessage(); // toastr()->error($th->getMessage()); // } return redirect()->route('user.index'); } /** * Show the specified resource. */ public function show($id) { $data['user'] = $this->userRepository->getUserById($id); return view('user::user.show'); } /** * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = "Edit User"; $data['editable'] = true; $data['roleLists'] = $this->roleRepository->pluck(); $data['user'] = $this->userRepository->getUserById($id); return view('crud.generated.users.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id): RedirectResponse { try { $validated = $request->validate([ 'name' => 'required|min:5', 'email' => 'required', 'password' => 'required', ]); $validated['password'] = bcrypt($validated['password']); $this->userRepository->update($id, $validated, $request->role); Toastr::success('User has been updated', 'Title', ["positionClass" => "toast-top-center"]); } catch (\Throwable $th) { toastr()->error($th->getMessage()); } return redirect()->route('user.index'); } /** * Remove the specified resource from storage. */ public function destroy($id) { try { $this->userRepository->delete($id); Toastr::success('User has been deleted', 'Title', ["positionClass" => "toast-top-center"]); } catch (\Throwable $th) { Toastr::success('Error deleting user', 'Title', ["positionClass" => "toast-top-center"]); } } }