title: Laravel Middleware not working on some resource route
body: <p>I am following a tutorial for making a access level restriction:</p>
  
  <p><a href="https://gist.github.com/amochohan/8cb599ee5dc0af5f4246" rel="nofollow noreferrer">https://gist.github.com/amochohan/8cb599ee5dc0af5f4246</a></p>
  
  <p>I was able to make it work somehow but there's something I need to get working which is not in the tutorial.</p>
  
  <p>Provided I have followed the tutorial. I have setup this resource route:</p>
  
  <pre><code>Route::group(['middleware' =&gt; ['auth', 'roles'], 'roles' =&gt; ['Administrator']], function()
  {
      Route::resource('changeschedule', 'ChangeScheduleController', ['only' =&gt; ['index'], 'except' =&gt; ['create']]);
  });
  </code></pre>
  
  <p>So what I wanted is just to apply the <code>roles</code> middleware to a resource route but with specific route in that resource only let's say I want to be applied in the <code>index</code> only so I have that route above.</p>
  
  <p>When I go to:</p>
  
  <pre><code>http://localhost/hrs/public/changeschedule
  </code></pre>
  
  <p>It works fine and the middleware <code>roles</code> is working fine. But why is that when I go to:</p>
  
  <pre><code>http://localhost/hrs/public/changeschedule/create
  </code></pre>
  
  <p>I am getting </p>
  
  <pre><code>NotFoundHttpException in RouteCollection.php line 161:
  </code></pre>
  
  <p>So I have a no found route error. Why is that? But when I do</p>
  
  <pre><code>Route::group(['middleware' =&gt; ['auth', 'roles'], 'roles' =&gt; ['Administrator']], function()
  {
      Route::resource('changeschedule', 'ChangeScheduleController');
  });
  </code></pre>
  
  <p>Then it works fine but the middleware is applied to all:</p>
  
  <pre><code>index, create, update, edit, delete
  </code></pre>
  
  <p>I want it to be in index only.</p>
  
  <p>My code:</p>
  
  <p>Kernel.php</p>
  
  <pre><code>protected $routeMiddleware = [
      'auth'          =&gt; \App\Http\Middleware\Authenticate::class,
      'auth.basic'    =&gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
      'guest'         =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
      'roles'         =&gt; \App\Http\Middleware\CheckRole::class,
  ];
  </code></pre>
  
  <p>CheckRole.php</p>
  
  <pre><code>&lt;?php namespace App\Http\Middleware;
  
  use Closure;
  
  class CheckRole{
  
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
  
      public function handle($request, Closure $next)
      {
          // Get the required roles from the route
          $roles = $this-&gt;getRequiredRoleForRoute($request-&gt;route());
          // Check if a role is required for the route, and
          // if so, ensure that the user has that role.
          if($request-&gt;user()-&gt;hasRole($roles) || !$roles)
          {
              return $next($request);
          }
          return response([
              'error' =&gt; [
                  'code' =&gt; 'INSUFFICIENT_ROLE',
                  'description' =&gt; 'You are not authorized to access this resource.'
              ]
          ], 401);
      }
  
      private function getRequiredRoleForRoute($route)
      {
          $actions = $route-&gt;getAction();
          return isset($actions['roles']) ? $actions['roles'] : null;
      }
  }
  </code></pre>