title: Laravel Permissions and Roles with Gate/Can
body: <p>at the moment i do a tutorial called "roles and permissions in laravel" from codecourse.com.</p>
  
  <p>In part 4 of the tutorial i have the problem i get everytime the result true.
  If i ask for the permission "edit posts" it should show me true, if i ask for the permission "delete posts" it should show me false.</p>
  
  <p>I checked the database relationship, but there is no relationship between the user and the permission "delete posts".</p>
  
  <p>Only if i ask for a permission that not exist like "blabla" (i mean not exist in the database) i got false.</p>
  
  <p>I believe he is only checking is there a permission with this name and not checking have the user the permission.</p>
  
  <p>web.php</p>
  
  <pre><code>Route::get('/', function (\illuminate\Http\Request $request) {
  $user = $request-&gt;user();
  
  dump($user-&gt;can("delete posts"));});
  </code></pre>
  
  <p>HasPermissionTrait.php</p>
  
  <pre><code>trait HasPermissionsTrait {
  
  public function hasRole(...$roles)
  {
      foreach ($roles as $role) {
          if ($this-&gt;roles-&gt;contains('name', $role)) {
              return true;
          }
      }
  
      return false;
  }
  
  public function hasPermissionTo($permission) {
      //Check has permission through role
  
      return $this-&gt;hasPermission($permission);
  }
  
  protected function hasPermission($permission) {
      return (bool) $this-&gt;permissions-&gt;where('name', $permission-&gt;name);
  }
  
  public function roles() {
      return $this-&gt;belongsToMany(Role::class, 'users_roles');
  
  }
  
  public function permissions() {
      return $this-&gt;belongsToMany(Permission::class, 'users_permissions');
  
  }
  </code></pre>
  
  <p>PermissionsServiceProvider.php</p>
  
  <pre><code>public function boot()
  {
      Permission::get()-&gt;map(function ($permission) {
          Gate::define($permission-&gt;name, function ($user) use ($permission) {
              return $user-&gt;hasPermissionTo($permission);
          });
      });
  }
  
  /**
   * Register the application services.
   *
   * @return void
   */
  public function register()
  {
      //
  }
  </code></pre>
  
  <p>you can watch my full code here -> <a href="https://github.com/RahmanG/joko" rel="nofollow noreferrer">https://github.com/RahmanG/joko</a></p>
  
  <p>On this image you can see the Auth. There is no permission "delete posts". But you can see the Gate is giving true.</p>
  
  <p><a href="https://imgur.com/a/gf923" rel="nofollow noreferrer">https://imgur.com/a/gf923</a></p>
  
  <p>Thank you for supporting </p>