Back to blog

Implementing a UI for Spatie Laravel Permissions

Feb 26, 2024
.
Samuel Trstenský

Are the users of your system all the same? Should each of your users be able to perform the same actions? I bet not. That's why you should consider implementing roles and permission management. But what is the correct approach for your Laravel project?

When considering the approach to implementing permissions in Laravel, most of us will probably turn to Spatie’s Laravel Permissions package. With just a hit of the enter key, you will have options like:

This is very cool, but soon you will realize that the only way to manage permissions is by using commands or directly through the database. Is that enough for you? If so, you can stop reading now :). However, it's more likely that you won't want to have permissions editable solely through the database. If you're working on a complex project, you'll need to consider creating a user-friendly UI for permission management.

You could start from scratch and create one on your own, but let's be honest, you probably don’t want to. You're looking to solve this task as quickly as possible. Don't worry, Craftable PRO has your back.

Craftable PRO’s Roles & Permissions Management

As someone once said, seeing is believing, so I want to show you how you can implement the Craftable PRO permissions UI in your existing project.

If you are starting your project from scratch with Craftable PRO, it comes directly with roles management— based on the Spatie Laravel permissions package — and a very nice UI.

However, there might be cases where we initially thought role management wouldn't be necessary, and manually creating roles would be enough. But then, the client realizes they want a nice UI and the ability to create custom roles with a custom set of permissions. In such cases, it's never too late for Craftable PRO! The roles and permissions will be automatically loaded, even if they are already defined.

Let's look at an example. Imagine you are developing a discussion forum. In this project, you will create a new custom role, which you will call 'Moderator'.

Role::create(['name' => 'Moderator']);

What is the role when it has no permissions? You will probably want the moderator to be able to show, edit, and delete comments. Let's define these permissions:

Permission::create(['name' => 'comment.show']); 
Permission::create(['name' => 'comment.edit']); 
Permission::create(['name' => 'comment.delete']);

Now, the dreadful news will arrive: the client wants a UI form permission management. But as you know, there's an easy solution ;).

Just add Craftable PRO into your project and call

php artisan craftable-pro:install

… and let the magic happen.

The magic

As you can see in the picture below, Craftable PRO automatically creates roles for Administrator and Guest and preserves previously created roles. You will also be able to create a new role directly from the Craftable PRO UI.

When you click on "Manage permissions", your existing permissions are loaded immediately, along with the default Craftable PRO permissions. You can see them in a neat grid, where you can easily attach or detach permissions to/from roles. Furthermore, if you use dot notation for permission names, Craftable PRO will automatically create groups based on the names and translate the permission names into a more human-friendly form.

You can even translate the role names in Localization.

Isn’t that amazing? Building a beautiful UI on top of Laravel Permissions from Spatie has never been easier. With automatic grouping based on permission names and translatability, it is the ideal solution for a Laravel admin panel with roles & permissions.

Conclusion

Roles & permissions management is an essential part of the majority of larger software projects. In Laravel, one way to implement this is by using Spatie Laravel Permissions. If editing roles & permissions management through code is not enough for you and you need a nice, powerful, and user-friendly UI, Craftable PRO's roles & permissions management is one of the tools that could be the right choice for your project. If you want to know more about how roles & permissions work in Craftable PRO, feel free to read our documentation as well.