52 lines
999 B
PHP
52 lines
999 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class Permission extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'group_name',
|
|
'description',
|
|
'sort',
|
|
];
|
|
|
|
protected $casts = [
|
|
'sort' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* 角色关联
|
|
*/
|
|
public function roles(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Role::class, 'role_permissions');
|
|
}
|
|
|
|
/**
|
|
* 按分组获取权限
|
|
*/
|
|
public static function getGroupedPermissions()
|
|
{
|
|
return static::orderBy('group_name')
|
|
->orderBy('sort')
|
|
->get()
|
|
->groupBy('group_name');
|
|
}
|
|
|
|
/**
|
|
* 通过slug查找
|
|
*/
|
|
public static function findBySlug(string $slug): ?self
|
|
{
|
|
return static::where('slug', $slug)->first();
|
|
}
|
|
}
|