I have 2 model in relationship
Tickets model
public function status(){ return $this->hasMany(Status::class); }
public function latestStatus(){ return $this->hasOne(Status::class)->latest();}
Status model
public function tickets(){ $this->belongsTo(Ticket::class);}
I am trying to get the latest status of each tickets using this code
public function scopeFilteredStatus($query){
return $query->whereHas('latestStatus',function($status){
$status->whereIn('status', ['status1,'status3','status6']);
})->get(); }
what ive got
IlluminateDatabaseEloquentCollection {#1609 ▼
#items: array:302 [?]
}
was expecting a fewer result
id --- user_id --- ticket_id --- status --- created_at
xx --- 10001 --- 3100 --- status1 --- 2020-01-13
xx --- 10001 --- 3200 --- status3 --- 2020-01-09
xx --- 10004 --- 2900 --- status6 --- 2020-01-20
and more...
question from:https://stackoverflow.com/questions/65896184/laravel-8-x-select-all-record-with-latest-status