SimpleRoles is a simple way to have roles in your laravel projects.
Via Composer
$ composer require merodiro/simple-roles
Run the command to publish the package migration
php artisan vendor:publish --provider="Merodiro\SimpleRoles\SimpleRolesServiceProvider"
Migrate database
It assumes that users are in the users
table, if not you can change the config file
php artisan migrate
add middleware in app/Http/Kernel.php
protected $routeMiddleware = [
...
'role' => \Merodiro\SimpleRoles\Middleware\RoleMiddleware::class,
];
add roles to roles
array in simple-roles config file first
$user->setRole('admin');
$user->removeRole();
if($user->hasRole('admin')){
// do something
}
to show content to admins only
@role('admin')
<h3>this is visible to admins only</h3>
@endrole
to show different content to admins and non-admins users
@role('admin')
<h3>this is visible to admins only</h3>
@else
<h3>this is visible to non admins only</h3>
@endrole
you can use middleware to limit accessing a certain route to admins only
Route::get('/admin', function () {
...
})->middleware('role:admin');
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.