Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the last record in a One to many Eloquent Relation using Laravel

Assuming there existed a One To Many relation where a User has Many Jobs, and the last record in the job table is the current job of the user. What is a better way of returning the users with their last jobs?

Here is what I have tried.

User Class

public function ejob(){
   return $this->hasMany(Ejob::class);
}

Ejob Class

public function user(){
   return $this->belongsTo(User::class);
}

API Controller Method

public function index()
{
  return UserResource::collection(( 
  User::with(
          $this->particulars() // I want the last record from this line
        )->orderBy('id', 'desc')->get() ));
}

Particulars Method

// 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'
        ];
}

User Resource

public function toArray($request)
    {
        //return parent::toArray($request);
        return [
            'data' => [
                'id' => $this->id,
                'surname' => $this->surname,
                'first_name' => $this->first_name,
                'other_name' => $this->other_name,
                'email' => $this->email,
                'phone_number' => $this->phone_number,
                'birthday' => $this->birthday->format('d-m-Y'),
                'age'=> $this->birthday->age,
                'ejob' => $this->whenLoaded('ejob'),
        ];
    }

Currently, this returns a user with all related records from the ejobs table but I want just the last job.

like image 461
codervine Avatar asked Dec 16 '19 08:12

codervine


People also ask

What is use of latest () in Laravel?

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.


1 Answers

You can use first() instead of get(). So it'll get a single model instance. get() method give a collection and first() method give you a single model instance.

User::with(
          $this->particulars()
        )->orderBy('id', 'desc')->first()

Or you can use latest() to get the last inserted record.

User::with(
          $this->particulars()
        )->latest()->first()

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the created_at column to chronologically order the data.

Edit:-

As you wanted to get the last record of the relationship you can do as below.

User::with('ejob', function($query) {
    return $query->latest()->first();
})->get();
like image 72
Dilip Hirapara Avatar answered Oct 06 '22 02:10

Dilip Hirapara