Laravel based roles for Laravel 6+.
composer require echods/roles
For Laravel version less than 5.8 please use tag branch 0.9.8
Making the shift to use handle instead of name. A name attribute was added for backwards compatibility hopefully to help. To upgrade don't forget to run the following:
$ php artisan migrate
$ php artisan vendor:publish --provider="Echods\Roles\RoleServiceProvider" --tag=config
Add the roles you need for your application and descriptions in the config file. Also change if you would like big integer migrations or note.
'admin' => [
'name' => 'admin',
'description' => 'Admin role for all stuff'
],
'editor' => [
'name' => 'editor',
'description' => 'Editor role for all stuff'
]
],
'migrations' => [
'useBigInteger' => true
]
Run migrations for roles table and role_user to be created.
$ php artisan migrate
This will fill the database with roles from the config file.
$ php artisan roles:generate
use Echods\Roles\Traits\HasRole;
class User extends Authenticatable
{
use HasRole;
//...
}
You can check roles of a user from the list of roles you have in the config. For example if you have roles admin
, editor
, employee
then you can do the following. Returns boolean.
$user = User::find(1);
$user->isAdmin();
$user->isEditor();
$user->isEmployee();
Alternatively you can do the following:
$user->hasRole('editor');
Checking if a user has multiple roles
$user->hasRoles(['admin', 'editor']);
// or
$user->hasRoles(['editor', 'admin']);
$user->attachRole('superAdmin');