New-OMIS/Modules/User/app/Http/Controllers/RoleController.php

94 lines
2.3 KiB
PHP
Raw Normal View History

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