Roles and Permissions

index.blade.php

@extends('layouts.admin')
@section('content')
<tab-nav class="mb-4">
<tab-nav-button title="All Users" url="{{ route('admin.users.index') }}"></tab-nav-button>
<tab-nav-button is-active title="Roles and Permissions"></tab-nav-button>
</tab-nav>
<card title="{{ $_title }}" action-url="{{ route('admin.roles.create') }}" action-label="Create Role"
has-middle-section>
<template v-slot:middle>
<tableable-actions ref="tableableactions"
:sort="[{name: 'created_at', asc: '(newest)', desc: '(oldest)', label: 'Created At'}, {name: 'name', asc: '(asc)', desc: '(desc)', label: 'Name'}]">
<template v-slot:bulk="props">
<dropdown-button>Select admin</dropdown-button>
</template>
<template v-slot:filter>
<alpine inline-template :populate-data="{ name: '{{ request('filter.name') }}' }">
<vfg :model="data">
{
type: "input",
inputType: "text",
label: 'Search',
inputName: "filter[name]",
model: "name",
required: true,
placeholder: ""
},
</vfg>
</alpine>
</template>
</tableable-actions>
</template>
<div class="flex flex-col">
<tableable ref="table" actions-ref="tableableactions" :items='@json($_data)'
class="tw-table is-tableable is-actionable is-bulkable">
<table>
<thead>
<tr>
<th>
</th>
<th>
Role Name
</th>
<th>
Users
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($_data as $data)
<tr>
<td>
<tableable-checkbox></tableable-checkbox>
</td>
<td>{{ $data->name }}</td>
<td>{{ $data->users->count() }}</td>
<td>
<a href="@route('admin.roles.edit', $data)" class="text-indigo-600 hover:text-indigo-900">Edit</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</tableable>
</div>
<pagination :paginator='@json($_data)'></pagination>
</div>
</card>
@endsection

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>