sunxyw/laravel-quick-role

A package to quickly build a simple roles manager system

v1.0.5 2020-08-27 06:56 UTC

This package is auto-updated.

Last update: 2022-04-27 11:06:47 UTC


README

A package to quickly build a simple roles manager system.

Installing

$ composer require sunxyw/laravel-quick-role

Usage

First, add the Sunxyw\LaravelQuickRole\HasRole trait to your User model:

use Sunxyw\LaravelQuickRole\HasRole;

class User extends Authenticatable
{
    use HasRole;
}

Then, create a new role for testing:

use Sunxyw\LaravelQuickRole\Models\Role;

Role::create([
    'name' => 'admin',
    'title' => 'Administrator',
    'color' => 'FF5555',
]);

Now, you can assign a role to a user by:

$user = User::find(1);
$user->assignRole('admin'); // By name
$user->assignRole(Role::find(1)); // By instance
$user->assignRole(1); // By ID

You can check user's role by:

$user->hasRole('admin'); // Accept name, ID and instance
// or
$user->hasAnyRole(['admin', 'leader']);

License

MIT