title: Laravel check user role on function construct
body: <p>I want to make the entire controller only available for admins, but when I try to access a normal user account, they can still access methods in the <code>AdminController</code>:</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\Auth;
  
  class AdminController extends Controller
  {
      public function __construct(Request $request)
      {
          $this-&gt;middleware('auth');
  
          if (!Auth::guest())
          {
              if (!$request-&gt;user()-&gt;hasRole('admin'))
              {
                  return redirect('/');
              }
          } 
  
          else 
          {
              return redirect('/');
          }
      }
  
      public function index()
      {
          return view('admin.index');
      }
  }
  </code></pre>
  
  <p>And here is the <code>User</code> model where it checks the roles:</p>
  
  <pre><code>public function roles()
      {
          return $this-&gt;belongsToMany(Role::class);
      }
  
      public function hasRole($role)
      {
          return null !== $this-&gt;roles()-&gt;where('name', $role)-&gt;first();
      }
  </code></pre>