validate([ "name" => "required", "description" => "nullable", ]); DB::beginTransaction(); Role::create($request->all()); DB::commit(); return redirect()->route("roles.index")->with('success','Succesfully Created'); } catch(\Exception $e){ DB::rollBack(); return redirect()->back()->with("error", $e->getMessage()); } } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { $role = Role::findOrFail($id); return view("roles.edit", compact('role')); } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { try{ $role = Role::findOrFail($id); // Validate request data $validatedData = $request->validate([ 'name' => 'required|string|max:255|unique:roles,name,' . $id, // Ensure name is unique except for the current role 'description' => 'nullable|string|max:500', ]); DB::beginTransaction(); $role->update($validatedData); DB::commit(); return redirect()->route('roles.index')->with('success','Successfully updated'); }catch(\Exception $e){ DB::rollBack(); return redirect()->back()->with("error", $e->getMessage()); } } /** * Remove the specified resource from storage. */ public function destroy(string $id) { try{ DB::beginTransaction(); Role::findOrFail($id)->delete(); DB::commit(); }catch(\Exception $e){ DB::rollBack(); return redirect()->back()->with("error", $e->getMessage()); } } }