-
-
Notifications
You must be signed in to change notification settings - Fork 86
Closed
Description
Is there any way to do this?
$users = User::currentStatus()->get();
$users = User::latestStatus()->get();
or other ways to join the status table and get the latest status. I have this in my model idk if its good enough or there are other way.
public function scopeWithLatestStatuses($query)
{
$latestStatuses = DB::table('statuses')
->select('model_id', DB::raw('MAX(id) as latest_status_id'))
->where('model_type', UserModel::class)
->groupBy('model_id');
$query->leftJoinSub(
$latestStatuses,
'latest_statuses',
function ($join) {
$join->on('users.id', '=', 'latest_statuses.model_id');
}
)->leftJoin('statuses', 'statuses.id', '=', 'latest_statuses.latest_status_id');
return $query;
}
Metadata
Metadata
Assignees
Labels
No labels