title: How to add multiple roles to a route group laravel
body: <p>I'm trying to add multiple roles to a route group in my <code>web</code> file in Laravel.
  I would like protect certain routes based on the users Role like the admin section.</p>
  <p>Some Routes need multiple roles for access.</p>
  <p><strong>Example Roles:</strong></p>
  <ul>
  <li>SuperAdmin</li>
  <li>Admin</li>
  <li>Moderator</li>
  <li>etc..</li>
  </ul>
  <p>This works and I can go to the admin panel if loged in and have <em><strong>One Role</strong></em> only in the route group.</p>
  <pre><code>Route::middleware(['auth','role:SuperAdmin'])-&gt;group(function () {
  Secret routes..
  });
  </code></pre>
  <blockquote>
  <p>This does not work if I try and add more roles to to the route group
  like this: <code>Route::middleware(['auth','role:SuperAdmin|Admin|Moderator'])-&gt;group(function () {</code></p>
  </blockquote>
  <p>Note the <code>'role:SuperAdmin|Admin|Moderator'</code></p>
  <hr />
  <p><strong>This is the RoleMiddleware file:</strong></p>
  <pre><code>    public function handle($request, Closure $next, $role, $permission = null)
      {
          if (!$request-&gt;user()-&gt;hasRole($role)) {
              abort(404);
          }
  
          if ($permission !== null &amp;&amp; !$request-&gt;user()-&gt;can($permission)) {
              abort(404);
          }
  
          return $next($request);
      }
  </code></pre>
  <hr />
  <p>The <code>User Class</code> has a trait called <code>use:HasPermissionTrait</code></p>
  <pre><code>class User extends Authenticatable
  {
      use Notifiable, HasPermissionsTrait, Billable;
  </code></pre>
  <p><strong>In that HasPermissionsTrait I have the following:</strong>
  I have permission setup fine, just focusing on the Roles in this file. I moved the Role logic to the top.</p>
  <pre><code>use App\{Role, Permission};
  
  trait HasPermissionsTrait
  {
      public function hasRole(...$roles)
      {
          foreach ($roles as $role) {
              if ($this-&gt;roles-&gt;contains('name', $role)) {
                  return true;
              }
          }
  
          return false;
      }
  
      public function roles()
      {
          return $this-&gt;belongsToMany(Role::class, 'users_roles');
      }
      
      ... // Permission logic start here...
  }
  </code></pre>
  <hr />
  <p><strong>Worth Mentioning:</strong>
  The tables for roles are:</p>
  <ul>
  <li>users</li>
  <li>roles</li>
  <li>users_roles (I know the correct naming convention should be <code>roles_users</code>, but not relevant here)</li>
  </ul>
  <blockquote>
  <p>Just need to know how to get this working in the route group:
  <code>'role:SuperAdmin|Admin|Moderator'</code></p>
  </blockquote>
  <hr />
  <p><strong>Solution:</strong>
  RoleMiddleware file:</p>
  <pre><code>    public function handle($request, Closure $next, $role, $permission = null)
      {
          $role = strtolower( $request-&gt;user()-&gt;hasRole($role));
          $allowed_roles = array_slice(func_get_args(), 2);
          
          if (!$request-&gt;user()-&gt;hasRole(in_array($role, $allowed_roles))) {
              abort(404);
          }
  
          if ($permission !== null &amp;&amp; !$request-&gt;user()-&gt;can($permission)) {
              abort(404);
          }
  
          return $next($request);
      }
  </code></pre>
  <p>Can do route group like this.
  <code>Route::middleware(['auth','role:SuperAdmin|Admin'])-&gt;group(function () {</code></p>