32 lines
392 B
PHP
32 lines
392 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Traits;
|
||
|
|
||
|
use App\Models\Role;
|
||
|
|
||
|
trait HasRole
|
||
|
{
|
||
|
|
||
|
private $roleClass = Role::class;
|
||
|
|
||
|
function role()
|
||
|
{
|
||
|
return $this->belongsTo($this->roleClass);
|
||
|
}
|
||
|
|
||
|
function getRole()
|
||
|
{
|
||
|
return $this->role()->first();
|
||
|
}
|
||
|
|
||
|
function roleName()
|
||
|
{
|
||
|
return $this->getRole()->name;
|
||
|
}
|
||
|
|
||
|
function isRole($name): bool
|
||
|
{
|
||
|
return $this->roleName() === $name;
|
||
|
}
|
||
|
}
|