Implementing a UI for Spatie Laravel Permissions
Tags: laravel, webdev, php
Published: 2024-03-21 12:52:22

If you are building complex admin panels, you will sooner or later encounter issues with users' permissions. And if you are using Laravel for your project, this article will demonstrate one of the most common methods for implementing Roles & Permission Management with a user-friendly UI. So, 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](https://spatie.be/docs/laravel-permission) package. With just a hit of the enter key, you will have options like:

- Defining roles
- Defining permissions for roles
- Attaching roles to the user model
- Option to restrict access based on permissions
- Blade directives to restrict access to parts of blade files

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](https://craftable.pro) has your back.

Craftable PRO is a Laravel admin panel and CRUD generator. It can help you not only with roles & permissions management but also with generating the complete admin panel from the database structure, managing media, handling translations, and more. It can simplify the process of building your entire Laravel backend.

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdfgqpcoupzktep81ggs.png)

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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciuqgfzxr6b7vw6bra33.png)
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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syseb9c3ssnes7iqvpo5.png)You can even translate the role names in Localization.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbq3e5zbl0gjq2hm9p3v.png)

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.