laravel-roles/src/Traits/HasRoles.php

33 lines
658 B
PHP
Raw Normal View History

2025-06-15 09:56:28 +00:00
<?php
namespace Blax\Roles;
trait HasRoles
{
/**
* The roles that belong to the model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(
config('permissions.models.role'),
config('permissions.table_names.model_has_roles'),
'model_id',
'role_id'
);
}
/**
* Check if the model has a specific role.
*
* @param string $role
* @return bool
*/
public function hasRole($role)
{
return $this->roles()->where('name', $role)->exists();
}
}