<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Halim Dirgantara</title>
    <description>The latest articles on DEV Community by Muhammad Halim Dirgantara (@halimdirgantara).</description>
    <link>https://dev.to/halimdirgantara</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F959631%2Fda8ef382-8fff-49a5-b157-1adf9ea38db5.png</url>
      <title>DEV Community: Muhammad Halim Dirgantara</title>
      <link>https://dev.to/halimdirgantara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/halimdirgantara"/>
    <language>en</language>
    <item>
      <title>Generate automatic permission names from routes with spatie Laravel-permissions</title>
      <dc:creator>Muhammad Halim Dirgantara</dc:creator>
      <pubDate>Mon, 31 Oct 2022 14:31:17 +0000</pubDate>
      <link>https://dev.to/halimdirgantara/generate-automatic-permission-names-from-routes-with-spatie-laravel-permissions-mk9</link>
      <guid>https://dev.to/halimdirgantara/generate-automatic-permission-names-from-routes-with-spatie-laravel-permissions-mk9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;disclaimer: This article is taken from &lt;a href="https://medium.com/@halimdirgantara/generate-automatic-permission-names-from-routes-with-spatie-laravel-permissions-5661d5bbcc4a"&gt;my article&lt;/a&gt; which has been published in the medium without being modified&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Laravel, Laravel-Spatie is a package that is widely used as a tool for managing users, roles and permissions in the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://spatie.be/docs/laravel-permission/v5/introduction"&gt;Spatie Laravel-Permission Introiduction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Before we use laravel-spatie please follow the prerequisites in laravel-spatie for important considerations regarding your User models!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://spatie.be/docs/laravel-permission/v5/prerequisites"&gt;Spatie Laravel-Permission Prerequisites&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can follow the installation documentation from this link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://spatie.be/docs/laravel-permission/v5/installation-laravel"&gt;Spatie Laravel-Permission Installation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After we install laravel-spatie then we can directly create a function to generate permissions from the existing route in our application.&lt;/p&gt;

&lt;p&gt;Create a seeder using the name PermissionSeeder by using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:seeder PermissionSeeder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Previously if we were going to create a seeder for the permission name like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//user permission View | Create | Edit | Delete =&amp;gt; USER
Permission::create([‘name’ =&amp;gt; ‘view user’]);
Permission::create([‘name’ =&amp;gt; ‘create user’]);
Permission::create([‘name’ =&amp;gt; ‘edit user’]);
Permission::create([‘name’ =&amp;gt; ‘update user’]);
Permission::create([‘name’ =&amp;gt; ‘delete user’]);
//user permisson View | Create | Edit | Delete =&amp;gt; ROLE
Permission::create([‘name’ =&amp;gt; ‘view role’]);
Permission::create([‘name’ =&amp;gt; ‘create role’]);
Permission::create([‘name’ =&amp;gt; ‘edit role’]);
Permission::create([‘name’ =&amp;gt; ‘update role’]);
Permission::create([‘name’ =&amp;gt; ‘delete role’]);
//user permisson View | Create | Edit | Delete =&amp;gt; PERMISSION
Permission::create([‘name’ =&amp;gt; ‘view permission’]);
Permission::create([‘name’ =&amp;gt; ‘create permission’]);
Permission::create([‘name’ =&amp;gt; ‘edit permission’]);
Permission::create([‘name’ =&amp;gt; ‘update permission’]);
Permission::create([‘name’ =&amp;gt; ‘delete permission’]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will replace it with a function like this,&lt;br&gt;
First, we retrieve data from the route in our application with this command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$routeCollection = Route::getRoutes()-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KYl6vWmM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5kn2vzafn85t6i2h1kz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KYl6vWmM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5kn2vzafn85t6i2h1kz.png" alt="data retrieved from route" width="632" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foreach ($routeCollection as $item) {
   //.... processing collection item
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CLQ0F7cS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7vfhnv18m9rfy3t9r98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CLQ0F7cS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7vfhnv18m9rfy3t9r98.png" alt="every item has this data" width="621" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we will retrieve the value of the array action of the item using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foreach ($routeCollection as $item) {
$name = $item-&amp;gt;action;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2PZT76JY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1vme9aaogz8odwg8v8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2PZT76JY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1vme9aaogz8odwg8v8o.png" alt="array of action in every item" width="616" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we will check every action array that we get whether it has an “as” variable, because not every action array has it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(!empty($name[‘as’])) {
.....//processing item
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$permission = $name[‘as’];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we change the case of the permission variable value to lowercase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$permission = trim(strtolower($permission));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we use the preg_replace function to convert each period, dash into a space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Permission::create([
    ‘name’ =&amp;gt; $permission
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the name of the permission that we get by using the steps above&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KRufqjCQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8drvwp526qxqe5hmn5y2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KRufqjCQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8drvwp526qxqe5hmn5y2.png" alt="Image description" width="475" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The overall code is as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$routeCollection = Route::getRoutes()-&amp;gt;get();
foreach ($routeCollection as $item) {
$name = $item-&amp;gt;action;
  if(!empty($name[‘as’])) {
    $permission = $name[‘as’];
    $permission = trim(strtolower($permission));
    $permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
    Permission::create([
        ‘name’ =&amp;gt; $permission
     ]);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have the permission names we generated from the route, we can assign each permission to the role we have.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You can follow the development of the functions in this article via this github link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/halimdirgantara/laravel-9-fortify-stisl"&gt;Github Laravel 9 Fortify Stisla using Spatie Laravel-Permission&lt;/a&gt;a&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>programming</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
