Roles and Permissions

Prepare User model

use Spatie\Permission\Traits\HasRoles;

template.blade.php

@extends('layouts.admin')
@section('content')
<enable-warn></enable-warn>
<card class="mb-5">
<template v-slot:title>
<breadcrumbs>
<breadcrumb label="All Roles" url="{{ route('admin.roles.index') }}"></breadcrumb>
<breadcrumb label="{{ $_title }}"></breadcrumb>
</breadcrumbs>
</template>
</card>
<alpine inline-template :populate-data='{ model: @json($_model), options: { validateAfterChanged: true } }'>
<div>
<form action="{{ $_action }}" method="POST">
<prepare-form></prepare-form>
<card title="{{ $_title }}" subtitle="Role is an important boi.">
<template v-slot:content>
@errors
<vfg is-row :model="data.model" :options="data.options" ref="form3">
{
type: "input",
inputType: "text",
inputName: "name",
label: "Role Name",
model: "name",
styleClasses: 'col-md-6',
placeholder: ""
}
</vfg>
</template>
<div class="flex flex-col">
<div class="tw-table is-tableable">
<table>
<thead>
<tr>
<th>
Permission
</th>
<th>
Allow
</th>
</tr>
</thead>
<tbody>
@foreach ($permissions as $permission)
<tr>
<td>
{{ $permission->name }}
</td>
<td>
<tableable-checkbox name="permissions[{{ $permission->name }}]" value="true" {{ $role->hasPermissionTo($permission) ? ':checked="true"' : '' }} {{ $role->id == 1 ? 'disabled' : '' }}></tableable-checkbox>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<template v-slot:after-content>
<card-actions @delete="warnBeforeSubmit($refs.deleteForm)"></card-actions>
</template>
</card>
</form>
<form ref="deleteForm" action="{{ $_deleteAction }}" method="POST">
@csrf
@method('Delete')
</form>
</div>
</alpine>
@endsection

Controller.php

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Vortechron\Essentials\Models\Role;
class RoleController extends Controller
{
public function index()
{
Role::indexAction();
return view('admin.user.role.index');
}
public function create()
{
Role::createAction(route('admin.roles.store'));
return view('admin.user.role.template');
}
public function store(Request $request)
{
$role = Role::storeAction($request);
return $this->handleRedirect(route('admin.roles.edit', $role), route('admin.roles.index'));
}
public function edit(Role $role)
{
Role::editAction($role, route('admin.roles.update', $role), route('admin.roles.destroy', $role), route('admin.roles.index'));
return view('admin.user.role.template');
}
public function update(Request $request, Role $role)
{
Role::updateAction($role, $request);
return $this->handleRedirect(route('admin.roles.edit', $role), route('admin.roles.index'));
}
public function destroy(Role $role)
{
Role::destroyAction($role);
return redirect()->route('admin.roles.index');
}
}

web.php

Route::resource('roles', 'RoleController');

On your user template

<div class="form-group">
<label for="role">Role</label>
<select class="form-control" name="role" id="role">
@foreach (Vortechron\RPManager\Models\Role::all() as $role)
<option value="{{ $role->name }}">{{ $role->name }}</option>
@endforeach
</select>
</div>