2025-06-15 09:56:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Blax\Roles;
|
|
|
|
|
|
|
|
|
|
trait HasRoles
|
|
|
|
|
{
|
|
|
|
|
/**
|
2025-06-15 16:29:50 +00:00
|
|
|
* Get all roles for the user.
|
2025-06-15 09:56:28 +00:00
|
|
|
*
|
2025-06-15 16:29:50 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
2025-06-15 09:56:28 +00:00
|
|
|
*/
|
|
|
|
|
public function roles()
|
|
|
|
|
{
|
2025-06-15 16:29:50 +00:00
|
|
|
return $this->morphToMany(
|
|
|
|
|
config('permissions.models.role', \Blax\Roles\Models\Role::class),
|
|
|
|
|
'member',
|
|
|
|
|
config('permissions.table_names.role_members', 'role_members')
|
2025-06-15 09:56:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-15 16:29:50 +00:00
|
|
|
* Check if the user has a specific role.
|
2025-06-15 09:56:28 +00:00
|
|
|
*
|
2025-06-15 16:29:50 +00:00
|
|
|
* @param string $roleSlug
|
2025-06-15 09:56:28 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2025-06-15 16:29:50 +00:00
|
|
|
public function hasRole(string $roleSlug): bool
|
2025-06-15 09:56:28 +00:00
|
|
|
{
|
2025-06-15 16:29:50 +00:00
|
|
|
return $this->roles()->where('slug', $roleSlug)->exists();
|
2025-06-15 09:56:28 +00:00
|
|
|
}
|
|
|
|
|
}
|