cms/app/Models/Role.php

47 lines
1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['name', 'slug', 'description', 'is_protected'];
/**
* The permissions that belong to the role.
*/
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany(User::class);
}
/**
* Boot the model.
*/
protected static function boot()
{
parent::boot();
static::deleting(function ($role) {
if ($role->is_protected) {
throw new \Exception("The protected '{$role->name}' role cannot be deleted.");
}
});
static::updating(function ($role) {
if ($role->is_protected && $role->isDirty('is_protected')) {
throw new \Exception("The protection status of '{$role->name}' cannot be modified.");
}
});
}
}