create user role and menu, create seeder for first user and create crud role, menu and user
This commit is contained in:
103
app/Http/Controllers/RolesController.php
Normal file
103
app/Http/Controllers/RolesController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Role;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RolesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view("roles.index");
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view("roles.create");
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
try{
|
||||
$request->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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user