Users roles for Laravel applications. Compatible with Laravel 7.x and 8.x.
This package provides you to set of middleware, migrations, directives and methods to user model which can you use for build Laravel Application wht different types of users. You can block or allow to use part of your application to for example admin or guest.
- First download package
composer require stanfortonski/laravel-roles
. - Setup provider. In config/app.php add following code to bottom of providers:
'providers' => [
//...
Stanfortonski\Laravelroles\ServiceProvider::class
],
- Setup middleware. In app/Http/Kernel.php add following code to bottom of middlewares:
protected $routeMiddleware = [
//...
'role' => \Stanfortonski\Laravelroles\Middleware\Role::class,
'roles' => \Stanfortonski\Laravelroles\Middleware\OneOfRoles::class,
'allofroles' => \Stanfortonski\Laravelroles\Middleware\AllOfRoles::class
];
- Use one of three traits HasRoles, HasRolesIds or HasRolesIdsAdapter in User class. Snippet:
use HasRoles;
,use HasRolesIds;
oruse HasRolesIdsAdapter;
. - Additonal you can publish config file roles.php. Run command:
php artisan vendor:publish --provider="Stanfortonski\Laravelroles\ServiceProvider"
.
-
HasRoles is for roles based on names. Default way.
-
HasRolesIds is for roles based on ids (methods suffix is ById or ByIds). HasRolesIds is actually for combining with HasRoles.
-
HasRolesIdsAdapter is for roles based on ids but methods are exactly same samed as in HasRoles trait. It is for independent use without HasRolesIds or HasRoles. Purpose of that is middlewares and directives doesn't work with HasRoleIds methods.
-
For HasRolesIds you have to use suffix ByIds for multiple or ById for singular. For parameter pass integer or array of integers. Example:
$user->hasRoleById(1)
instead of$user->hasRole('nameofrole')
-
For HasRolesIdsAdapter you doesn't have to use suffix but for parameter you have to pass integer or array of integers. Example:
$user->hasRole(1)
instead of$user->hasRole('nameofrole')
If you want to determine which user can use the link. You need to use one of three middleware: roles, roles, allofroles.
Examples: (Attention you have to define admin, moderator and writer roles before! Go To Seeding example.)
- Only admin can access to main page.
Route::get('/', function () {
return view('welcome');
})->middleware('role:admin');
- Only Admin or moderator can access to main page.
Route::get('/', function () {
return view('welcome');
})->middleware('roles:admin|moderator');
- Only User that have admin and writer roles (all of these) can access to main page.
Route::get('/', function () {
return view('welcome');
})->middleware('allofroles:admin|writer');
$result = $user->hasRole('admin');
$result = $user->hasOneOfRoles(['admin', 'mod']);
$result = $user->hasAllOfRoles(['admin', 'mod']);
$user->addRole('admin');
$user->addRoles(['admin', 'writer']);
$user->removeRole('admin');
$user->removeRoles(['admin', 'mod']);
If you want to define yours own roles run this command php artisan make:seeder RoleSeeder
and next copy and paste below code to database/seeders/RoleSeeder.php.
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class RoleSeeder extends Seeder
{
private $roles = [
['name' => 'admin', 'description' => 'User friendly text'], //role I
//... next role
];
public function run()
{
foreach ($this->roles as $role){
DB::table('roles')->insert($role);
}
}
}
Then run composer dump-autoload
and php artisan migrate --seed
.