title: How to authenticate specific route to user with specific role in laravel Milldeware
body: <p>I have multiple users with multiple permissions. A user can belong to the only single role but that role can have multiple permissions like <code>create</code>, <code>read</code>, <code>update</code>, <code>delete</code>. And I have a <code>RoleMiddleware</code>. I am authenticating the user in <code>roleMiddleware</code>. But how can I protect <code>routes</code> in <code>RoleMiddleware</code> against a specific user?</p>
  
  <p>For Example, I have a route <code>create-case</code> which can only be accessed by the <code>operator</code> or by <code>Admin</code> else everyone redirects to <code>404 error</code> how Can I deal with it in <code>RoleMiddleware</code>.</p>
  
  <p>I have written basic code for authentication where every user with their roles is authenticated but I am getting how can I code in middleware so ever route when a user hits it may go to the <code>RoleMiddleware</code> where middleware <code>Authenticate</code> route to the Role and then give him the access.</p>
  
  <p>Role Middleware</p>
  
  <pre><code>class RoleMiddleware
  {
      public function handle($request, Closure $next, $permission = null)
      {
  
          if (Auth::check() === false)
          {
              return redirect('login');
          }
          elseif (Auth::check() === true)
          {
              $roles = Role::all()-&gt;pluck('slug');
  
              if (is_null($request-&gt;user()) )
              {
                  abort(404);
              }
              if (!$request-&gt;user()-&gt;hasRole($roles))
              {
                  abort(404);
              }
  
              if ($request-&gt;user())
              {
                  if ($request-&gt;user()-&gt;hasRole($roles))
                  {
                      return $next($request);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Case Controller:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Cases;
  
  use App\Http\Controllers\Controller;
  use App\Http\Requests\CaseStoreRequest;
  use Illuminate\Support\Facades\Auth;
  use Session;
  
  class CaseController extends Controller
  {
      use DropzoneFileUploadTraits;
  
      public function __construct()
      {
          $this-&gt;middleware('role');
  
      }
  
      public function index()
      {
          $data['portal'] = Portal::all();
          $data['operators'] = Operator::all();
  
          return view('case', $data);
      }
  
      public function caseList()
      {
          $user = new User();
          $isAdmin = $user-&gt;isAdmin();
  
          $loggedIn = Auth::id();
  
          $cases = Cases::with('patients', 'portal')
              -&gt;when(!$isAdmin, function ($query) use ($loggedIn) {
                  return $query-&gt;where('user_id', $loggedIn);
              })-&gt;orderBy('created_at', 'desc')-&gt;get();
  
          $data['cases'] = $cases;
  
          return view('case_list', $data);
      }
  }
  </code></pre>
  
  <p>Route:</p>
  
  <pre><code>Route::get('create-case', 'Cases\CaseController@index')-&gt;name('create-case');
  Route::post('case-submit', 'Cases\CaseController@caseSubmit')-&gt;name('case-submit');
  Route::post('edit-patient-case-submit', 'Cases\CaseController@editPatientCaseSubmit')-&gt;name('edit-patient-case-submit');
  Route::get('case-list', 'Cases\CaseController@caseList')-&gt;name('case-list');
  </code></pre>