Generate automatic permission names from routes with spatie Laravel-permissions
Tags: laravel, programming, php, tutorial
Published: 2022-10-31 14:31:17

> disclaimer: This article is taken from [my article](https://medium.com/@halimdirgantara/generate-automatic-permission-names-from-routes-with-spatie-laravel-permissions-5661d5bbcc4a) which has been published in the medium without being modified

In Laravel, Laravel-Spatie is a package that is widely used as a tool for managing users, roles and permissions in the database.

[Spatie Laravel-Permission Introiduction](https://spatie.be/docs/laravel-permission/v5/introduction)

We just need to create roles and permissions to synchronize with the user, so we can set what roles can perform a function or what permissions are allowed to perform a function.

In this article we will not discuss how to install laravel or how to install the laravel-spatie package which you can directly check on the respective documentation pages, but we will directly try to create a function in the seeder that is able to create permissions automatically without having to create them one by one manually.

Before we use laravel-spatie please follow the prerequisites in laravel-spatie for important considerations regarding your User models!

[Spatie Laravel-Permission Prerequisites](https://spatie.be/docs/laravel-permission/v5/prerequisites)

You can follow the installation documentation from this link

[Spatie Laravel-Permission Installation](https://spatie.be/docs/laravel-permission/v5/installation-laravel)

After we install laravel-spatie then we can directly create a function to generate permissions from the existing route in our application.

Create a seeder using the name PermissionSeeder by using this command:

```
php artisan make:seeder PermissionSeeder
```

Previously if we were going to create a seeder for the permission name like this:

```
//user permission View | Create | Edit | Delete => USER
Permission::create([‘name’ => ‘view user’]);
Permission::create([‘name’ => ‘create user’]);
Permission::create([‘name’ => ‘edit user’]);
Permission::create([‘name’ => ‘update user’]);
Permission::create([‘name’ => ‘delete user’]);
//user permisson View | Create | Edit | Delete => ROLE
Permission::create([‘name’ => ‘view role’]);
Permission::create([‘name’ => ‘create role’]);
Permission::create([‘name’ => ‘edit role’]);
Permission::create([‘name’ => ‘update role’]);
Permission::create([‘name’ => ‘delete role’]);
//user permisson View | Create | Edit | Delete => PERMISSION
Permission::create([‘name’ => ‘view permission’]);
Permission::create([‘name’ => ‘create permission’]);
Permission::create([‘name’ => ‘edit permission’]);
Permission::create([‘name’ => ‘update permission’]);
Permission::create([‘name’ => ‘delete permission’]);
```

Then we will replace it with a function like this,
First, we retrieve data from the route in our application with this command :

```
$routeCollection = Route::getRoutes()->get();
```

![data retrieved from route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5kn2vzafn85t6i2h1kz.png)

We save the array that we got from that array into routeCollection variable, and then we will iterate over each data in the array for us to process with the command

```
foreach ($routeCollection as $item) {
   //.... processing collection item
}
```

![every item has this data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7vfhnv18m9rfy3t9r98.png)

we will retrieve the value of the array action of the item using the command:

```
foreach ($routeCollection as $item) {
$name = $item->action;
}
```

![array of action in every item](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1vme9aaogz8odwg8v8o.png)

Next we will check every action array that we get whether it has an “as” variable, because not every action array has it.

```
if(!empty($name[‘as’])) {
.....//processing item
}
```

if the action array has a value of “as” then we will process it using the following string function, but first we save the value of the variable name with the value “as” into the variable permission

```
$permission = $name[‘as’];
```

we change the case of the permission variable value to lowercase

```
$permission = trim(strtolower($permission));
```

then we use the preg_replace function to convert each period, dash into a space.

```
$permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
```

then the value obtained is a word with lowercase letters and separated by spaces.
We will save the value of the variable into the permissions table in the database using the create . function

```
Permission::create([
    ‘name’ => $permission
]);
```

Here is the name of the permission that we get by using the steps above

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

The overall code is as follows :

```
$routeCollection = Route::getRoutes()->get();
foreach ($routeCollection as $item) {
$name = $item->action;
  if(!empty($name[‘as’])) {
    $permission = $name[‘as’];
    $permission = trim(strtolower($permission));
    $permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
    Permission::create([
        ‘name’ => $permission
     ]);
  }
}
```

Once we have the permission names we generated from the route, we can assign each permission to the role we have.

In the next article, we will discuss how to do so that on each existing route we can check permissions based on the route on each function in the controller.

You can follow the development of the functions in this article via this github link

[Github Laravel 9 Fortify Stisla using Spatie Laravel-Permission](https://github.com/halimdirgantara/laravel-9-fortify-stisl)a