title: Better way to include custom fields from user table in JWT claim in laravel
body: <p>I am using <code>tymon/jwt-auth</code> liibrary for <code>JWT authentication</code> in my project which is built using <code>laravel</code>. </p>
  
  <p>In my mysql <code>users</code> table I am having a <code>role_id</code> column. I want to include that in my generated <code>JWT token's claim</code> on login so that I can implement role based <code>ACL</code> using my JWT token on subsquent requests. </p>
  
  <p>Below is what I have tried to get it working</p>
  
  <pre><code>    $credentials = $request-&gt;only('email', 'password');
      $userModel = User::where('email',$request-&gt;input('email'))-&gt;get()-&gt;first();
      $role = $userModel-&gt;roles_id;
      $customClaim = ['role' =&gt; $role];
  
      try 
      {
          // attempt to verify the credentials and create a token for the user
          if (! $token = JWTAuth::attempt($credentials, $customClaim)) 
          {
              //return 401 error
          }
      } 
      catch (JWTException $e) 
      {
          //return 500 error
      }
  
      return response()-&gt;json(compact('token'));
  }        
  </code></pre>
  
  <p>Above code is working fine as expected but problem with above code is on login I have to hit my <code>database</code> twice once for getting role and one for authenticating user. That would be great if someone can suggest a better way to do it in a single database hit.</p>