New-OMIS/Modules/User/app/Http/Controllers/RoleController.php
2024-04-09 18:00:26 +05:45

94 lines
2.3 KiB
PHP

<?php
namespace Modules\User\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\User\Repositories\RoleRepository;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class RoleController extends Controller
{
private $roleRepository;
public function __construct(RoleRepository $roleRepository){
$this->roleRepository = $roleRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['roles'] = $this->roleRepository->findAll();
$data['editable'] = false;
return view('user::role.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['permissions'] = Permission::get();
$data['title'] = "Create Role";
return view('user::role.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate(['name' => 'required']);
$role = $this->roleRepository->create($request->all());
$role->syncPermissions($request->permissions);
toastr()->success('New role has been created!');
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(Role $role)
{
$data['permissions'] = Permission::get();
$data['role'] = $role;
$data['title'] = "Edit Role";
return view('user::role.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Role $role)
{
$role->update(['name' => $request->name]);
$role->syncPermissions($request->permissions);
toastr()->success('Role has been updated!');
return redirect()->route('role.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Role $role)
{
$role->delete();
toastr()->success('Role has been deleted!');
}
}