laravel-roles maintained by nietthijmen
Description
This is my package laravel-roles
Last update
2026/05/12 04:57
(dev-dependabot/composer/illuminate/contracts-tw-13.8.0)
Downloads
0
Tags
Laravel roles
This package provides a simple and easy way to manage roles in your laravel package.
Installation
- Install the package via composer:
composer require nietthijmen/laravel-roles
- Run the install command to publish the configuration file:
php artisan roles:install
- Grab a cup of coffee and wait for the package to be installed.
- Add the
HasRolestrait to your User model:
use Nietthijmen\LaravelRoles\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
- Alias your middleware (if you want to use this)
// bootstrap/app.php
withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'role' => \NietThijmen\LaravelRoles\Http\Middleware\RoleMiddleware::class
]);
})
- Add the RoleServiceProvider
// bootstrap/providers.php
<?php
return [
//...
\NietThijmen\LaravelRoles\Providers\RoleServiceProvider::class
];
- You're good to go 🎉
Usage
Blade directive
@role('admin')
<p>You are an admin!</p>
@endrole
Middleware
You can use the middleware to protect your routes:
Route::middleware(['role:admin'])->group(function () {
Route::get('/admin', function () {
return 'You are an admin!';
});
});
Manual usage
auth()->user()->hasRole('admin'); // returns true if the user has the admin role