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.

Laravel Packages Laravel Laravel News

Similar Articles

PHP develop your own package with local composer environment

PHP develop your own package with local composer environment

Developing your own PHP composer package locally & integrating local file symlink within your application can speeds up development immensely.
Read more...
Laravel Zero: A Micro-framework for console applications

Laravel Zero: A Micro-framework for console applications

Get leverage of laravel magic to create console applications within a micro framework.
Read more...
Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

Browsershot is package to convert webpage to image or PDF in laravel.
Read more...