Files
hyperf_rbac_framework_serve…/app/Model/AdminMenu.php
2025-09-17 16:17:55 +08:00

114 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
use App\Model\Meta\AdminUserMeta;
use App\Model\Meta\MetaCast;
use Carbon\Carbon;
use Hyperf\Database\Model\Events\Deleting;
use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Collection;
/**
* @property int $id
* @property int $parent_id
* @property string $name
* @property AdminUserMeta $meta
* @property string $path
* @property string $component
* @property string $redirect
* @property int $status
* @property int $sort
* @property int $created_by
* @property int $updated_by
* @property Carbon $created_at
* @property Carbon $updated_at
* @property string $remark
* @property Collection|AdminRole[] $roles
* @property Collection|AdminMenu[] $children
*/
class AdminMenu extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'admin_menu';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [
'id',
'parent_id',
'name',
'component',
'redirect',
'status',
'sort',
'created_by',
'updated_by',
'created_at',
'updated_at',
'remark',
'meta',
'path',
];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = [
'id' => 'integer',
'parent_id' => 'integer',
'status' => AdminMenuStatusCode::class,
'sort' => 'integer',
'created_by' => 'integer',
'updated_by' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'meta' => MetaCast::class,
'path' => 'string',
];
/**
* 通过中间表获取角色.
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(
AdminRole::class,
'admin_role_belongs_menu',
'admin_menu_id',
'admin_role_id'
);
}
/**
* @return HasMany
*/
public function children(): HasMany
{
return $this
->hasMany(self::class, 'parent_id', 'id')
->where('status', AdminMenuStatusCode::Normal)
->orderBy('sort')
->with('children');
}
/**
* @param Deleting $event
* @return void
*/
public function deleting(Deleting $event): void
{
$this->roles()->detach();
}
}