nietthijmen/laravel-roles

This is my package laravel-roles

1.0.0 2025-06-13 14:23 UTC

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

  1. Install the package via composer:
composer require nietthijmen/laravel-roles
  1. Run the install command to publish the configuration file:
php artisan roles:install
  1. Grab a cup of coffee and wait for the package to be installed.
  2. Add the HasRoles trait to your User model:
use Nietthijmen\LaravelRoles\Traits\HasRoles;
class User extends Authenticatable
{
    use HasRoles;

    // ...
}
  1. 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
        ]);
})
  1. Add the RoleServiceProvider
// bootstrap/providers.php
<?php

return [
    //...
    \NietThijmen\LaravelRoles\Providers\RoleServiceProvider::class
];
 
  1. 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