New in Laravel 8.42: In an Eloquent model can define a relation that will get the newest (or oldest) item of another relation.
1public function historyItems(): HasMany 2{ 3 return $this 4 ->hasMany(ApplicationHealthCheckHistoryItem::class) 5 ->orderByDesc('created_at'); 6} 7 8public function latestHistoryItem(): HasOne 9{10 return $this11 ->hasOne(ApplicationHealthCheckHistoryItem::class)12 ->latestOfMany();13}