title: Return the last record in a One to many Eloquent Relation using Laravel
body: <p>Assuming there existed a <code>One To Many</code> relation where a User has Many Jobs, and the last record in the <code>job</code> table is the current job of the user. What is a better way of returning the users with their last jobs?</p>
  
  <p>Here is what I have tried.</p>
  
  <p><code>User Class</code></p>
  
  <pre><code>public function ejob(){
     return $this-&gt;hasMany(Ejob::class);
  }
  </code></pre>
  
  <p><code>Ejob Class</code></p>
  
  <pre><code>public function user(){
     return $this-&gt;belongsTo(User::class);
  }
  </code></pre>
  
  <p><code>API Controller Method</code></p>
  
  <pre><code>public function index()
  {
    return UserResource::collection(( 
    User::with(
            $this-&gt;particulars() // I want the last record from this line
          )-&gt;orderBy('id', 'desc')-&gt;get() ));
  }
  </code></pre>
  
  <p><code>Particulars Method</code></p>
  
  <pre><code>// I want the last record from this
  private function particulars(){
          return 
          [
              'ejob.company:id,name', 
              'ejob.job:id,title', 
              'ejob.department:id,name',
              'ejob.reporting:id,surname,first_name,other_name',
              'ejob.employmentstatus:id,name', 
              'country:id,name', 
              'gender:id,name', 
              'state:id,name'
          ];
  }
  </code></pre>
  
  <p><code>User Resource</code></p>
  
  <pre><code>public function toArray($request)
      {
          //return parent::toArray($request);
          return [
              'data' =&gt; [
                  'id' =&gt; $this-&gt;id,
                  'surname' =&gt; $this-&gt;surname,
                  'first_name' =&gt; $this-&gt;first_name,
                  'other_name' =&gt; $this-&gt;other_name,
                  'email' =&gt; $this-&gt;email,
                  'phone_number' =&gt; $this-&gt;phone_number,
                  'birthday' =&gt; $this-&gt;birthday-&gt;format('d-m-Y'),
                  'age'=&gt; $this-&gt;birthday-&gt;age,
                  'ejob' =&gt; $this-&gt;whenLoaded('ejob'),
          ];
      }
  </code></pre>
  
  <p>Currently, this returns a user with all related records from the <code>ejobs</code> table but I want just the last job.</p>