Laravel permissioning system(Role based access control)

Laravel permissioning system(Role based access control)

Mar 04, 2019 / Ramesh Mhetre

Using ACL is a much common requirement for almost every project ranging from few users to big CRM's.

Laravel has multiple options for using ACL for your project or website.

Spatie has a nice & simple package for role based ACL which works with your existing user model. This package allows you to manage user permissions and roles in a database.

Once installed you can do stuff like this:

// Adding permissions to a user
$user->givePermissionTo('edit articles');

// Adding permissions via a role
$user->assignRole('writer');

If you're using multiple guards, every guard will have its own set of permissions and roles that can be assigned to the guard's users. You can even this package with Laravel & Lumen.

Because all permissions will be registered on Laravel's gate, you can check if a user has a permission with Laravel's default can function:

Here are some examples how we can use it :

// You may also pass an array
$user->givePermissionTo(['edit articles', 'delete articles']);

$user->revokePermissionTo('edit articles');

$user->syncPermissions(['edit articles', 'delete articles']);

$user->hasPermissionTo('edit articles');

$user->hasPermissionTo('1');
$user->hasPermissionTo(Permission::find(1)->id);
$user->hasPermissionTo($somePermission->id);

$user->hasAnyPermission(['edit articles', 'publish articles', 'unpublish articles']);

There are so many things you can do with with package. Head over & add it to your project.


Similar Articles

Connect your laravel application to Sqlite DB

Connect your laravel application to Sqlite DB

Quickly connecting with sqlite database within laravel application
Read more...
Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 is is releasing soon. Serverless laravel is next big thing in laravel community!
Read more...
Compiling assets with Laravel Mix

Compiling assets with Laravel Mix

Laravel Mix provides a fluent API for defining Webpack build steps
Read more...