Bulletinboard/app/Http/Controllers/RoleController.php

116 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Role;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
class RoleController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
* @return View
*/
public function index(Request $request): View
{
$roles = Role::query()->paginate($request->input("limit", 20));
return view("roles-index",["roles" => $roles]);
}
/**
* Show the form for creating a new resource.
* @return View
*/
public function create(): View
{
return view("admin.create-role");
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request): RedirectResponse
{
$rawdata = $request->validate([
'name' => 'required|string'
]);
$role = new Role($rawdata);
if($role->save()){
return redirect()->route("index");
}
return redirect()->route("error");
}
/**
* Display the specified resource.
*
* @param Role $role
* @return View
*/
public function show(Role $role): View
{
return view("admin.show-role",[
"role" => $role
]);
}
/**
* Show the form for editing the specified resource.
* @param Role $role
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|View|\Illuminate\Http\Response
*/
public function edit(Role $role)
{
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Role $role
* @return view
*/
public function update(Request $request, Role $role): View
{
$rawdata = $request->validate([
'name' => 'required|string'
]);
$role->update($rawdata);
if(!$role->save()){
return view("admin.show-role",[
'role' => $role
]);
}
else{
return view("admin.show-role",[
'role' => $role,
"success" => "Opdaterede role"
]);
}
}
/**
* Remove the specified resource from storage.
*
* @param Role $role
* @return RedirectResponse
*/
public function destroy(Role $role): RedirectResponse
{
$role->delete();
return redirect()->route("index");
}
}