2.9 Menu roles Resource (ok)
Last updated
Last updated
C:\xampp82\htdocs\testnet\app\Http\Controllers\RolesController.php
<?php
namespace App\Http\Controllers;
use App\Models\Menurole;
use App\Models\RoleHierarchy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
class RolesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$roles = DB::table('roles')
->leftJoin('role_hierarchy', 'roles.id', 'role_hierarchy.role_id')
->select('roles.*', 'role_hierarchy.hierarchy')
->orderBy('hierarchy', 'asc')
->get();
return view("roles.index")->with(compact('roles'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('roles.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$role = new Role();
$role->name = $request->input('name');
$role->save();
$hierarchy = RoleHierarchy::select('hierarchy')
->orderBy('hierarchy', 'desc')->first();
if (empty($hierarchy)) {
$hierarchy = 0;
} else {
$hierarchy = $hierarchy['hierarchy'];
}
$hierarchy = ((int)$hierarchy) + 1;
$roleHierarchy = new RoleHierarchy();
$roleHierarchy->role_id = $role->id;
$roleHierarchy->hierarchy = $hierarchy;
$roleHierarchy->save();
$request->session()->flash('message', 'Successfully created role');
return redirect()->route('roles.create');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return view('roles.show', array(
'role' => Role::where('id', $id)->first()
));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return view('roles.edit', array(
'role' => Role::where('id', $id)->first()
));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$role = Role::where('id', $id)->first();
$role->name = $request->input('name');
$role->save();
$request->session()->flash('message', 'Successfully updated role');
return redirect()->route('roles.edit', $id);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
$role = Role::where('id', '=', $id)->first();
$roleHierarchy = RoleHierarchy::where('role_id', $id)->first();
$menuRole = Menurole::where('role_name', $role->name)->first();
if (!empty($menuRole)) {
$request->session()->flash('message', "Can't delete. Role has assigned one or more menu elements.");
$request->session()->flash('back', 'roles.index');
return view('dashboard.shared.universal-info');
} else {
$role->delete();
$roleHierarchy->delete();
$request->session()->flash('message', "Successfully deleted role");
$request->session()->flash('back', 'roles.index');
return view('dashboard.shared.universal-info');
}
}
}
C:\xampp82\htdocs\testnet\resources\views\roles\create.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="fade-in">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h4>Create new role</h4>
</div>
<div class="card-body">
@if(Session::has('message'))
<div class="alert alert-success" role="alert">{{ Session::get('message') }}</div>
@endif
<form method="POST" action="{{ route('roles.store') }}">
@csrf
<table class="table table-bordered datatable">
<tbody>
<tr>
<th>
Name
</th>
<td>
<input class="form-control" name="name" type="text" />
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" type="submit">Save</button>
<a class="btn btn-primary" href="{{ route('roles.index') }}">Return</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
C:\xampp82\htdocs\testnet\resources\views\roles\edit.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="fade-in">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h4>Edit role</h4>
</div>
<div class="card-body">
@if(Session::has('message'))
<div class="alert alert-success" role="alert">{{ Session::get('message') }}</div>
@endif
<form method="POST" action="{{ route('roles.update', $role->id) }}">
@csrf
@method('PUT')
<input type="hidden" name="id" value="{{ $role->id }}" />
<table class="table table-bordered datatable">
<tbody>
<tr>
<th>
Name
</th>
<td>
<input class="form-control" name="name" value="{{ $role->name }}" type="text" />
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" type="submit">Save</button>
<a class="btn btn-primary" href="{{ route('roles.index') }}">Return</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
C:\xampp82\htdocs\testnet\resources\views\roles\index.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="fade-in">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h4>Menu roles</h4>
</div>
<div class="card-body">
<div class="row">
<a class="btn btn-lg btn-primary" href="{{ route('roles.create') }}">Add new role</a>
</div>
<br>
<table class="table table-striped table-bordered datatable aaa9">
<thead>
<tr>
<th>Name</th>
<th>Hierarchy</th>
<th>Created at</th>
<th>Updated at</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($roles as $role)
<tr>
<td>
{{ $role->name }}
</td>
<td>
{{ $role->hierarchy }}
</td>
<td>
{{ $role->created_at }}
</td>
<td>
{{ $role->updated_at }}
</td>
<td>
<a class="btn btn-success" href="#">
<i class="cil-arrow-thick-top"></i>
</a>
</td>
<td>
<a class="btn btn-success" href="#">
<i class="cil-arrow-thick-bottom"></i>
</a>
</td>
<td>
<a href="{{ route('roles.show', $role->id ) }}" class="btn btn-primary">Show</a>
</td>
<td>
<a href="{{ route('roles.edit', $role->id ) }}" class="btn btn-primary">Edit</a>
</td>
<td>
<form action="{{ route('roles.destroy', $role->id ) }}" method="POST">
@method('DELETE')
@csrf
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
C:\xampp82\htdocs\testnet\resources\views\roles\show.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="fade-in">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h4>Menu roles</h4>
</div>
<div class="card-body">
<table class="table table-striped table-bordered datatable aaa10">
<thead>
<tr>
<th>Name</th>
<th>Created at</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{ $role->name }}
</td>
<td>
{{ $role->created_at }}
</td>
<td>
{{ $role->updated_at }}
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary" href="{{ route('roles.index') }}">Return</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection