74 lines
2.0 KiB
PHP
Executable File
74 lines
2.0 KiB
PHP
Executable File
<?php
|
|
namespace App\Models\Auth;
|
|
|
|
use Dcat\Admin\Models\Role;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class RoleModel extends Role
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::bootTraits();
|
|
static::deleting(function ($model) {
|
|
$model->rPermissions()->delete();
|
|
|
|
$model->rMenus()->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* A role belongs to many users.
|
|
*
|
|
* @return BelongsToMany
|
|
*/
|
|
public function administrators() :BelongsToMany
|
|
{
|
|
$pivotTable = config('admin.database.role_users_table');
|
|
|
|
$relatedModel = config('admin.database.users_model');
|
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id')
|
|
->withoutTrashed()->whereNull($pivotTable.'.deleted_at');
|
|
}
|
|
|
|
/**
|
|
* A role belongs to many permissions.
|
|
*
|
|
* @return BelongsToMany
|
|
*/
|
|
public function permissions() :BelongsToMany
|
|
{
|
|
$pivotTable = config('admin.database.role_permissions_table');
|
|
|
|
$relatedModel = config('admin.database.permissions_model');
|
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id')->withTimestamps()
|
|
->withoutTrashed()->whereNull($pivotTable.'.deleted_at');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany
|
|
*/
|
|
public function menus() :BelongsToMany
|
|
{
|
|
$pivotTable = config('admin.database.role_menu_table');
|
|
|
|
$relatedModel = config('admin.database.menu_model');
|
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'menu_id')->withTimestamps()
|
|
->withoutTrashed()->whereNull($pivotTable.'.deleted_at');
|
|
}
|
|
|
|
public function rPermissions() {
|
|
return $this->hasMany(RolePermissionModel::class, 'role_id', 'id');
|
|
}
|
|
|
|
public function rMenus() {
|
|
return $this->hasMany(RoleMenuModel::class, 'role_id', 'id');
|
|
}
|
|
}
|
|
|