title: How to get a specific field value from a with() in laravel 9
body: <p>I have an API in which I want to add some specific values to JWT, the data comes from a table <code>tbl_user</code> which is connected to a different table <code>tbl_user_details</code>, via foreign key <code>user_id</code>. I have a relation between them using <em>belongsTo</em> and <em>hasMany</em>. The <em>tbl_user_details</em> contains a field <strong>first_name</strong>, which I want to get as a data for a variable <code>$first_name</code>. so that I can add it to JWT.</p>
  <h2>User Model</h2>
  <pre><code>public function details()
  {
      return $this-&gt;hasMany(UserDetails::class, 'user_id', 'user_id');
  }
  </code></pre>
  <h2>UserDetails Model</h2>
  <pre><code>public function belongs_to_user()
  {
      return $this-&gt;belongsTo(User::class, 'user_id', 'user_id');
  }
  </code></pre>
  <p>On calling the below controller,</p>
  <h4>Controller</h4>
  <pre><code>$first_variable = User::where('user_id', $user_id)-&gt;with(['details' =&gt; function ($query) {
        $query-&gt;select('first_name', 'user_id');
  }])
  -&gt;get();
  
  return response()-&gt;json([
        'first_out' =&gt; $first_variable,
  ]);
  </code></pre>
  <p>I get the json output on postman as below,</p>
  <h4>Postman output</h4>
  <pre><code>&quot;first_out&quot;: [
      {
        &quot;user_id&quot;: 1,
        &quot;email&quot;: &quot;il.comn@gmail.c&quot;,
        &quot;mobile&quot;: &quot;RN91919191&quot;,
        &quot;role_id&quot;: 1,
        &quot;user_status&quot;: &quot;active&quot;,
        &quot;created_date&quot;: &quot;2020-07-29T18:30:00.000000Z&quot;,
        &quot;updated_date&quot;: &quot;2020-07-29T18:30:00.000000Z&quot;,
        &quot;details&quot;: [
          {
            &quot;first_name&quot;: &quot;Test_User&quot;,
            &quot;user_id&quot;: 1
          }
        ]
      }
  </code></pre>
  <p>But I only want the output as below</p>
  <h4>Expected output</h4>
  <pre><code>&quot;first_out&quot;: &quot;Test_User&quot;
  </code></pre>
  <p>What should I do, Please help.</p>
  <p>Thanks in advance.</p>