2.3 Create table role_hierarchy (ok)
Last updated
Last updated
C:\xampp82\htdocs\testvn\app\Http\Middleware\GetMenu.php
<?php
namespace App\Http\Middleware;
use App\Http\Menus\GetSidebarMenu;
use App\Models\Menulist;
use App\Models\RoleHierarchy;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class GetMenu
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$role = 'guest';
$userRoles = Auth::user()->getRoleNames();
$roleHierarchy = RoleHierarchy::join('roles', 'role_hierarchy.hierarchy', '=', 'roles.id')
->orderBy('role_hierarchy.hierarchy', 'asc')
->get();
$flag = false;
foreach ($roleHierarchy as $roleHier) {
foreach ($userRoles as $userRole) {
if ($userRole == $roleHier['name']) {
$role = $userRole;
$flag = true;
break;
}
}
if ($flag === true) {
break;
}
}
} else {
$role = 'guest';
}
$menulists = Menulist::all();
$menus = new GetSidebarMenu();
$result = [];
foreach ($menulists as $menulist) {
$result[$menulist->name] = $menus->get($role, $menulist->id);
}
view()->share('appMenus', $result);
return $next($request);
}
}
C:\xampp82\htdocs\testvn\database\migrations\2023_05_12_070519_create_role_hierarchy_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_hierarchy', function (Blueprint $table) {
$table->id();
$table->integer('role_id')->unsigned();
$table->integer('hierarchy');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_hierarchy');
}
};
C:\xampp82\htdocs\testvn\database\seeders\RoleHierarchySeeder.php
<?php
namespace Database\Seeders;
use App\Models\RoleHierarchy;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Spatie\Permission\Models\Role;
use Illuminate\Database\Seeder;
class RoleHierarchySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$adminRole = Role::where(['name' => 'admin'])->first();
RoleHierarchy::create([
'role_id' => $adminRole->id,
'hierarchy' => 1,
]);
$userRole = Role::where(['name' => 'user'])->first();
RoleHierarchy::create([
'role_id' => $userRole->id,
'hierarchy' => 2,
]);
$guestRole = Role::where(['name' => 'guest'])->first();
RoleHierarchy::create([
'role_id' => $guestRole->id,
'hierarchy' => 3,
]);
}
}
C:\xampp82\htdocs\testvn\database\seeders\DatabaseSeeder.php
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call([
PermissionSeeder::class,
UserRoleSeeder::class,
MenusTableSeeder::class,
RoleHierarchySeeder::class,
]);
}
}
C:\xampp82\htdocs\testvn\app\Models\RoleHierarchy.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RoleHierarchy extends Model
{
use HasFactory;
protected $table = 'role_hierarchy';
protected $fillables = [
'role_id',
'hierarchy'
];
}