laravel-roles/src/Traits/HasRoles.php

32 lines
683 B
PHP
Raw Normal View History

2025-06-15 09:56:28 +00:00
<?php
2025-06-16 06:31:14 +00:00
namespace Blax\Roles\Traits;
2025-06-15 09:56:28 +00:00
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(
2025-06-16 05:37:35 +00:00
config('roles.models.role', \Blax\Roles\Models\Role::class),
2025-06-15 16:29:50 +00:00
'member',
2025-06-16 05:37:35 +00:00
config('roles.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
}
}