nietthijmen / laravel-roles
This is my package laravel-roles
Fund package maintenance!
NietThijmen
1.0.0
2025-06-13 14:23 UTC
Requires
- php: ^8.4
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
This package is auto-updated.
Last update: 2025-06-13 21:36:20 UTC
README
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
HasRoles
trait 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