Download the PHP package soumen-dey/laravel-role without Composer
On this page you can find all versions of the php package soumen-dey/laravel-role. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download soumen-dey/laravel-role
More information about soumen-dey/laravel-role
Files in soumen-dey/laravel-role
Package laravel-role
Short Description A lightweight Access Control package for Laravel 5.6 and above.
License MIT
Homepage https://github.com/soumen-dey/laravel-role
Informations about the package laravel-role
laravel-role
A lightweight Access Control package for Laravel 5.6 and above.
This package allows you to manage roles for your users, its very lightweight and require no extra dependencies.
Installation
Via Composer
For Laravel 5.5 and above the service provider will automatically get registered. Still if it is not registered, just add the service provider in config/app.php
file.
Migrations
The migrations for this package will automatically run when you run the command.
Note: Make sure that you have your associated model table already migrated before using the command for this package.
Configurations
You need to publish the config file with:
This will publish file under the directory.
You can use this package without modifying the default configurations. The defaults are set to work with the Laravel's default model for auth which is the model. However you can change the configurations based on your need.
The configurations are:
Note: If you change the default model, make sure to change the model's table name and the pivot table name.
Tip: If you assign a null value to the pivot table name, this package will automatically generate the pivot table name for you.
Usage
- Setup
- Creating Roles
- Retrieving Roles
- Assigning Roles
- Revoking Roles
- Sync Roles
- Role Associations
- Determining Role Associations
- Using the Middleware
Setup
Add the to your model or any other model that you want to associate roles with. That's it! You are all set to go!
Creating Roles
You can create new roles:
You can pass an array of role names or several role names at once.
Retrieving Roles
You can retrieve the roles by one of these methods:
Retrieve a role by its
Retrieve a role by its
All these methods will throw a exception if a role is not found, to change this behavior pass a second optional argument as , in such case the method will return if a role is not found.
A role can also be created if not found:
Check if a role exists:
If a role exists, this method will return the instance else it will return .
Assigning Roles to the model
Roles can be easily assigned by using one of these methods:
You can also assign roles by their or their model instances:
You can also pass an array:
There is also an method that does the same thing.
Revoking roles from the model
Roles can be revoked or removed from the model by one of these methods:
By their
By their :
By the instance:
This package is very flexible, in an extreme scenario you can also do this and still it won't complain :) :
There is also a method that does the same thing.
Sync Roles
Roles can be removed at once by the above methods, but roles can be removed and assigned at the same time:
You can also pass an array of either role , or instance.
Role associations with the model
The trait adds Eloquent Relationship to the associated model, so you can do this:
Names of the associated roles can be fetched:
The trait also adds a role scope to your models to scope the query to certain roles:
It can be also used as:
Determining Role associations
Check if the model has any of the specified roles (OR):
Using the role :
Using the role :
Using the instance:
You can also do this:
There is another method available:
The only difference between and is that you can pass as many arguments as you like to the method.
Note that both these methods returns a .
Check if the model has all the specified roles (AND):
This method returns only if all the specified roles are associated with the model, else it returns .
The method
This method is a quick way of determining if a model has a certain role. It is a very simple method and is faster than the above methods (the performance difference is very small, almost negligible).
This method only accept one argument which is the of the role.
Using the Middleware
This package comes with middleware. You can add it inside your file.
You can protect your routes using the middleware:
You can also use the middleware in a single route:
You can specify multiple roles in the middleware by separating them with a comma:
Note: The above method will determine if the model has any one (OR) of the specified roles.
To determine if a model has all (AND) of the specified roles, use the flag:
Note: The flag should be right after the middleware name, which in this case is . Thus the string should look like:
Using Blade Directive
By default, this package does not ship with any custom blade directive but you can add one easily. Assuming the default associated model is the model, just follow the steps:
In your add the following inside the method:
You can now use the directive:
For a more role specific directive:
You can now use the directive:
Or if you don't want any custom directive, you can do:
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email me at [email protected].
Credits
- Soumen Dey
- All Contributors
License
This package is released under the MIT License (MIT). Please see the license file for more information.