title: How to protect a route with middleware in laravel?
body: <p>i have a problem with my middleware. when i login as admin, it's working fine and redirect to <code>/Admin/home</code> same as Operator (i have 2 user, Admin &amp; Operator). The problem is when i hit url as example : <code>/Operator/home</code> as Admin role, it can access it. And that's the problem.</p>
  
  <p>I'have create a new middleware <code>CheckMiddleware</code>, and registered to kernel in array $routeMiddleware as checkMiddleware:</p>
  
  <pre><code>public function handle($request, Closure $next)
      {
          $user = $request-&gt;user();
  
          if ($user) {
              if ($user-&gt;isAdmin()) {
                  return $next($request);
              }elseif($user-&gt;isOperator()){
                  return $next($request);
              }
          }
  
          return dd('Forbidden page. you have to login as admin/operator');
      }
  </code></pre>
  
  <p>In the route :</p>
  
  <pre><code>Route::group(['prefix'=&gt;'Admin' ,'middleware' =&gt; 'checkMiddleware'], function() {
      Route::get('/home', 'HomeController@index')-&gt;name('homeAdmin');
  });
  
  Route::group(['prefix'=&gt;'Operator' ,'middleware' =&gt; 'checkMiddleware'], function() {
      Route::get('/home', 'HomeController@index')-&gt;name('homeAdmin');
  });
  
  Auth::routes();
  </code></pre>
  
  <p>in User model :</p>
  
  <pre><code>public function isAdmin(){
          if ($this-&gt;role_id === 1) {
              return true;
          }
  
          return false;
      }
  
      public  function isOperator(){
          if ($this-&gt;role_id === 2) {
              return true;
          }
  
          return false;
      }
  </code></pre>
  
  <p>What i want is, Admin cannot access Operator and Operator Cannot Access Admin. 
  if this is not clear, tell me what file you want to see.</p>