停车场管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

107 lines
2.8 KiB

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
class AdminMenu extends Model
{
use HasFactory, Notifiable, SoftDeletes;
protected $table = 'admin_menu';
/**
* The attributes that should be hidden for serialization.
* @var array<int, string>
*/
protected $hidden
= [
'updated_at',
'deleted_at',
];
// 查询菜单列表
public static function getMenuList(
int $parent_id = 0,
array $menu_ids = []
): array
{
$model = self::query();
if ($menu_ids) {
$model->whereIn('id', $menu_ids);
}
$list = $model->where('parent_id', $parent_id)->whereNull(
'deleted_at'
)->select(['id', 'title'])->get()->toArray();
foreach ($list as &$item) {
$item['title'] = empty($item['title'])
? ''
: __(
'menu.' . $item['title']
);
}
return $list;
}
// 获取目录id
public static function getMenuId($title, $is_child = false)
{
$model = self::query()->where('title', $title);
if ($is_child) {
$model->where('parent_id', '>', 0);
}
return $model->value('id');
}
// 获取目录名称
public static function getParentTitle($parent_id): string
{
$title = self::query()->where('id', $parent_id)->whereNull('deleted_at')->value('title');
return empty($title) ? '' : __('menu.' . $title);
}
// 获取上级目录名称
public static function getLastParentTitle($parent_id): string
{
$last_parent_id = self::query()->where('id', $parent_id)->whereNull('deleted_at')->value('parent_id');
$title = self::getTreeTitle($last_parent_id);
return empty($title) ? '' : __('menu.' . $title);
}
// 获取最高级目录名称
public static function getTreeTitle($id): string
{
$value = self::query()->whereNull('deleted_at')->find($id);
if ($value['parent_id'] > 0) {
return self::getTreeTitle($value['parent_id']);
}
return $value['title'];
}
/**
* @param $value
* @return string
*/
public function getCreatedAtAttribute($value): string
{
return get_datetime('datetime', strtotime($value));
}
/**
* A Menu belongs to many roles.
* @return BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(
AdminRoles::class,
AdminRoleMenu::class,
'menu_id',
'role_id'
);
}
}