Is there a better way to get the object with the lowest price with Laravel Eloquent? Each Product has several ticketTypes and each ticketType can have multiple prices.
public function includeLowestPrice(Product $product)
{
$lowestPriceTicket = null;
foreach ($product->ticketTypes()->hasActivePrices()->get() as $type) {
foreach ($type->prices()->get() as $ticketPrice) {
if (!$lowestPriceTicket || $ticketPrice->value < $lowestPriceTicket->value) {
$lowestPriceTicket = $ticketPrice;
}
}
}
return $lowestPriceTicket ? $this->item($lowestPriceTicket, new TicketPriceTransformer()) : null;
}