create user role and menu, create seeder for first user and create crud role, menu and user
This commit is contained in:
173
database/seeders/UsersRoleMenuSeeder.php
Normal file
173
database/seeders/UsersRoleMenuSeeder.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Menu;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class UsersRoleMenuSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$roles = [
|
||||
[
|
||||
"name" => "superadmin",
|
||||
"description" => "show all menus for super admins",
|
||||
],
|
||||
[
|
||||
"name" => "admin",
|
||||
"description" => "show only necessary menus for admins",
|
||||
],
|
||||
[
|
||||
"name" => "operator",
|
||||
"description" => "show only necessary menus for operators",
|
||||
]
|
||||
];
|
||||
|
||||
Role::upsert($roles, ['name']);
|
||||
|
||||
$parent_menus = [
|
||||
[
|
||||
"name" => "Dashboard",
|
||||
"url" => "/dashboard",
|
||||
"icon" => "mingcute:home-3-line",
|
||||
"parent_id" => null,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
[
|
||||
"name" => "Master",
|
||||
"url" => "/master",
|
||||
"icon" => "mingcute:cylinder-line",
|
||||
"parent_id" => null,
|
||||
"sort_order" => 2,
|
||||
],
|
||||
[
|
||||
"name" => "Settings",
|
||||
"url" => "/settings",
|
||||
"icon" => "mingcute:settings-6-line",
|
||||
"parent_id" => null,
|
||||
"sort_order" => 3,
|
||||
],
|
||||
[
|
||||
"name" => "Data Settings",
|
||||
"url" => "/data-settings",
|
||||
"icon" => "mingcute:settings-1-line",
|
||||
"parent_id" => null,
|
||||
"sort_order" => 4,
|
||||
],
|
||||
[
|
||||
"name" => "Data",
|
||||
"url" => "/data",
|
||||
"icon" => "mingcute:task-line",
|
||||
"parent_id" => null,
|
||||
"sort_order" => 5,
|
||||
]
|
||||
];
|
||||
|
||||
foreach ($parent_menus as $parent_menu) {
|
||||
Menu::create($parent_menu);
|
||||
}
|
||||
|
||||
// Attach Menus to Roles
|
||||
$superadmin = Role::where('name', 'superadmin')->first();
|
||||
$admin = Role::where('name', 'admin')->first();
|
||||
$operator = Role::where('name', 'operator')->first();
|
||||
|
||||
$dashboard = Menu::where('name', 'Dashboard')->first();
|
||||
$master = Menu::where('name', 'Master')->first();
|
||||
$settings = Menu::where('name', 'Settings')->first();
|
||||
$dataSettings = Menu::where('name', 'Data Settings')->first();
|
||||
$data = Menu::where('name', 'Data')->first();
|
||||
|
||||
// Superadmin gets all menus
|
||||
$superadmin->menus()->attach([$dashboard->id, $master->id, $settings->id, $dataSettings->id, $data->id]);
|
||||
|
||||
// Admin gets limited menus
|
||||
$admin->menus()->attach([$dashboard->id, $master->id, $settings->id]);
|
||||
|
||||
// Operator gets only basic menus
|
||||
$operator->menus()->attach([$dashboard->id, $data->id]);
|
||||
|
||||
// Attach User to role super admin
|
||||
User::findOrFail(1)->roles()->attach($superadmin->id);
|
||||
|
||||
// create children menu
|
||||
|
||||
// dashboard children
|
||||
$children_menus = [
|
||||
[
|
||||
"name" => "Dashboard Pimpinan",
|
||||
"url" => "dashboard.home",
|
||||
"icon" => null,
|
||||
"parent_id" => $dashboard->id,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
[
|
||||
"name" => "Dashboard PBG",
|
||||
"url" => "dashboard.pbg",
|
||||
"icon" => null,
|
||||
"parent_id" => $dashboard->id,
|
||||
"sort_order" => 2,
|
||||
],
|
||||
[
|
||||
"name" => "Users",
|
||||
"url" => "users.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $master->id,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
[
|
||||
"name" => "Syncronize",
|
||||
"url" => "settings.syncronize",
|
||||
"icon" => null,
|
||||
"parent_id" => $settings->id,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
[
|
||||
"name" => "Menu",
|
||||
"url" => "menus.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $settings->id,
|
||||
"sort_order" => 2,
|
||||
],
|
||||
[
|
||||
"name" => "Role",
|
||||
"url" => "roles.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $settings->id,
|
||||
"sort_order" => 3,
|
||||
],
|
||||
[
|
||||
"name" => "Assign Role Menu",
|
||||
"url" => "roles.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $settings->id,
|
||||
"sort_order" => 4,
|
||||
],
|
||||
[
|
||||
"name" => "Setting Dashboard",
|
||||
"url" => "data-settings.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $dataSettings->id,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
[
|
||||
"name" => "PBG",
|
||||
"url" => "pbg-task.index",
|
||||
"icon" => null,
|
||||
"parent_id" => $data->id,
|
||||
"sort_order" => 1,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($children_menus as $child_menu) {
|
||||
Menu::create($child_menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user