create menu migration, seeder and model

This commit is contained in:
Geriano
2022-07-16 04:29:57 +07:00
parent cff8ab1b72
commit 8e687de2d9
5 changed files with 237 additions and 0 deletions

63
app/Models/Menu.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use JsonSerializable;
use stdClass;
class Menu extends Model
{
use HasFactory;
/**
* @var string[]
*/
protected $fillable = [
'parent_id',
'name',
'icon',
'route_or_url',
'position',
'enable',
'actives',
'deleteable',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function parent()
{
return $this->hasOne(Menu::class, 'id', 'parent_id')->without(['childs'])->with(['parent']);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function childs()
{
return $this->hasMany(Menu::class, 'parent_id', 'id')->with(['parent', 'childs'])->orderBy('position');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
/**
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
public function actives() : Attribute
{
return Attribute::make(
get: fn ($value) => json_decode($value),
set: fn ($value) => is_array($value) || $value instanceof JsonSerializable || $value instanceof stdClass ? json_encode($value) : $value,
);
}
}

View 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');
}
};

View File

@@ -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');
}
};

View File

@@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder
{
$this->call([
InitialSeeder::class,
MenuSeeder::class,
]);
}
}

View 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'])
);
}
}