create menu migration, seeder and model
This commit is contained in:
46
database/migrations/2022_07_15_192421_create_menus_table.php
Normal file
46
database/migrations/2022_07_15_192421_create_menus_table.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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('menus', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('parent_id')
|
||||
->nullable()
|
||||
->default(null);
|
||||
$table->string('name');
|
||||
$table->string('icon')
|
||||
->default('circle');
|
||||
$table->string('route_or_url')->default('#');
|
||||
$table->unsignedTinyInteger('position');
|
||||
$table->boolean('enable')->default(true);
|
||||
$table->boolean('deleteable')->default(true);
|
||||
$table->json('actives')->default('[]');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('parent_id')
|
||||
->references('id')
|
||||
->on('menus');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('menus');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
<?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('menu_permission', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('menu_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
$table->foreignId('permission_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('menu_permission');
|
||||
}
|
||||
};
|
||||
@@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
$this->call([
|
||||
InitialSeeder::class,
|
||||
MenuSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
90
database/seeders/MenuSeeder.php
Normal file
90
database/seeders/MenuSeeder.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Menu;
|
||||
use App\Models\Permission;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class MenuSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$dashboard = Menu::create([
|
||||
'name' => 'dashboard',
|
||||
'icon' => 'tachometer-alt',
|
||||
'route_or_url' => 'dashboard',
|
||||
'position' => 1,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$builtin = Menu::create([
|
||||
'name' => 'builtin',
|
||||
'icon' => 'circle',
|
||||
'position' => 2,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$permission = $builtin->childs()->create([
|
||||
'name' => 'permission',
|
||||
'route_or_url' => 'superuser.permission.index',
|
||||
'icon' => 'key',
|
||||
'position' => 1,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$permission->permissions()->attach(
|
||||
Permission::whereIn('name', [
|
||||
'create permission', 'read permission', 'update permission', 'delete permission',
|
||||
])->get(['id'])
|
||||
);
|
||||
|
||||
$role = $builtin->childs()->create([
|
||||
'name' => 'role',
|
||||
'route_or_url' => 'superuser.role.index',
|
||||
'icon' => 'user-cog',
|
||||
'position' => 2,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$role->permissions()->attach(
|
||||
Permission::whereIn('name', [
|
||||
'create role', 'read role', 'update role', 'delete role',
|
||||
])->get(['id'])
|
||||
);
|
||||
|
||||
$user = $builtin->childs()->create([
|
||||
'name' => 'user',
|
||||
'route_or_url' => 'superuser.user.index',
|
||||
'icon' => 'user',
|
||||
'position' => 3,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$user->permissions()->attach(
|
||||
Permission::whereIn('name', [
|
||||
'create user', 'read user', 'update user', 'delete user',
|
||||
])->get(['id'])
|
||||
);
|
||||
|
||||
$menu = $builtin->childs()->create([
|
||||
'name' => 'menu',
|
||||
'route_or_url' => 'superuser.menu.index',
|
||||
'icon' => 'bars',
|
||||
'position' => 4,
|
||||
'deleteable' => false,
|
||||
]);
|
||||
|
||||
$menu->permissions()->attach(
|
||||
Permission::whereIn('name', [
|
||||
'create menu', 'read menu', 'update menu', 'delete menu',
|
||||
])->get(['id'])
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user