3. 1 List view roles (ok)
Last updated
Last updated
C:\xampp82\htdocs\phongkhamnet\app\Http\Controllers\RoleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class RoleController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$roles = Role::paginate(2);
return view('roles.index')->with(compact('roles'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$permission = Permission::get();
return view('roles.create', compact('permission'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:roles,name',
'permission' => 'required',
]);
$role = Role::create(['name' => $request->input('name')]);
$role->syncPermissions($request->input('permission'));
return redirect()->route('role.index');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$role = Role::find($id);
$rolePermissions = Permission::join("role_has_permissions", "permissions.id", "=","role_has_permissions.permission_id")
->where("role_has_permissions.role_id", $id)->get();
return view('roles.show')->with(compact('role','rolePermissions'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
C:\xampp82\htdocs\phongkhamnet\resources\views\users\index.blade.php
@extends('layouts.app')
@section('content')
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h1>Users Management</h1>
@can('user-create')
<a class="btn btn-primary" href="{{ route('user.create') }}" role="button">Create New User</a>
@endcan
<table class="table">
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Roles</th>
<th scope="col">Admin</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<th scope="row">1</th>
<td>{{ $user->name }}</td>
<td>{{ $user->emai }}</td>
<td>
@foreach ($user->roles as $role)
<span class="badge bg-secondary">{{ $role->name }}</span>
@endforeach
</td>
<td>
<a class="btn btn-info text-white" href="{{ route('user.show',$user->id) }}">Show</a>
@can('user-edit')
<a class="btn btn-primary text-white" href="{{ route('user.edit',$user->id) }}">Edit</a>
@endcan
@can('user-delete')
{!! Form::open(['method'=>'DELETE','route'=>['user.destroy',$user->id],'class'=>'d-inline']) !!}
{!! Form::submit("Delete", ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endcan
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection