title: Laravel permission cache
body: <p>I am using my own implementation for user roles and permissions in laravel how to cache all the permissions and roles on user login and also to refresh the cache when new record is added.
  My tables are users ,roles, permissions,permission_role,permission_user,role_user.  This is my table structure</p>
  
  <p>i have created on of the provider and added this code to boot method</p>
  
  <pre><code>Permission::get()-&gt;map(function ($permission) 
              {
                  Gate::define($permission-&gt;name, function ($user) use ($permission) 
                  {
                      return $user-&gt;hasPermission($permission);
                  });
              });
  </code></pre>
  
  <p>it is working fine but it is running the query each time which gets slowing down my application is there any way to cache all the permissions</p>
  
  <p>here is my PermissionServiceProvider class</p>
  
  <pre><code>&lt;?php
  
  namespace App\Providers;
  
  use Illuminate\Support\ServiceProvider;
  use Illuminate\Support\Facades\Gate;
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Support\Facades\Blade;
  use App\Models\Permission;
  
  class PermissionServiceProvider extends ServiceProvider
  {
      /**
       * Bootstrap services.
       *
       * @return void
       */
      public function boot()
      {
          if (Schema::hasTable('permissions'))
           {
              Permission::get()-&gt;map(function ($permission) 
              {
                  Gate::define($permission-&gt;name, function ($user) use ($permission) 
                  {
                      return $user-&gt;hasPermission($permission);
                  });
              });
          }
  
          Blade::directive('role', function ($role)
              {
                 return "&lt;?php if(Auth::user()-&gt;hasRole({$role})): ?&gt;";
              });
          Blade::directive('endrole', function ($role)
              {
                  return "&lt;?php endif; ?&gt;";
              });
      }
  
      /**
       * Register services.
       *
       * @return void
       */
      public function register()
      {
          //
      }
  }
  </code></pre>
  
  <p>and finally inside the user  model </p>
  
  <pre><code>public function hasPermission($permission)
      {
          return $this-&gt;hasPermissionThroughRole($permission) || (bool) $this-&gt;permissions-&gt;where('name',$permission-&gt;name)-&gt;count();
      }
  
      public function hasPermissionThroughRole($permission)
      {
          foreach($permission-&gt;roles as $role)
          {
              if($this-&gt;roles-&gt;contains($role))
              {
                  return true;
              }
          }
          return false;
      }
  </code></pre>
  
  <p>and also here is my bit bucket repo </p>
  
  <p><a href="https://manojkiran@bitbucket.org/manojkiran/userrolesandpermissions.git" rel="nofollow noreferrer">https://manojkiran@bitbucket.org/manojkiran/userrolesandpermissions.git</a></p>
  
  <p>i have tried the method of <code>@emtiaz-zahid</code>  <a href="https://stackoverflow.com/a/53511803/8487424">https://stackoverflow.com/a/53511803/8487424</a> its working fine to cache all the <code>permisisons</code> in <code>permissions</code> table but is there any way to cahe all the permisisons and also the permissions to specifc user and role of the currently logged in user</p>