title: User role wise permission for controller function in laravel
body: <p>I am working with laravel5.4. I need to create role wise permission for every method.</p>
  
  <p>Here I have created one middleware <strong>(roles)</strong> as below :</p>
  
  <p><strong>CheckRole.php</strong> </p>
  
  <pre><code>public function handle($request, Closure $next)
  {
      $role = array_slice(func_get_args(), 2);
      if ( ! User::find(auth()-&gt;id())-&gt;hasAnyRole($role))
      {    
          return response("Insufficient permission",401);
          session()-&gt;flash('message',"You have no permission to access");
          return redirect('/');
      }
      return $next($request);
  }
  </code></pre>
  
  <p>I have one controller as <strong>CompanyController</strong> :</p>
  
  <p><strong>CoompanyController.php</strong></p>
  
  <pre><code>public function __construct()
  {
      $this-&gt;middleware(function (Request $request, $next) 
      {
          $this-&gt;user = \Auth::User();
          //print_r($this-&gt;user);exit;
          if($this-&gt;user['userrole_id'] == 1)
          {
              $this-&gt;middleware('roles:superadmin');      
          }
          else if ($this-&gt;user['userrole_id'] == 3) 
          {
              $this-&gt;middleware('roles:supportperson',['only' =&gt; ['index']]);
          }
          return $next($request);
      });
  }
  </code></pre>
  
  <p>Here, if login user is <strong>superadmin</strong> than he can access all the function of controller. And If login user is <strong>supportperson</strong> than he can access only <strong>index</strong> method. </p>
  
  <p>So how can I set permission to function for different rolewise using same middleware?</p>