You can tweak your relations to get what you want. Accepted answer of course works, however it might be memory overkill with lots of data.
Find out more here and there.
Here's how to use Eloquent for this:
// Product model
public function latestPrice()
{
return $this->hasOne('Price')->latest();
}
// now we're fetching only single row, thus create single object, per product:
$products = Product::with('latestPrice')->get();
$products->first()->latestPrice; // Price model
That's nice, but there's more. Imagine you'd like to load highest price (just a value) for all the products:
public function highestPrice()
{
return $this->hasOne('Price')
->selectRaw('product_id, max(price) as aggregate')
->groupBy('product_id');
}
Not very convenient yet:
$products = Product::with('highestPrice')->get();
$products->first()->highestPrice; // Price model, but only with 2 properties
$products->first()->highestPrice->aggregate; // highest price we need
So add this accessor to make the life easier:
public function getHighestPriceAttribute()
{
if ( ! array_key_exists('highestPrice', $this->relations)) $this->load('highestPrice');
$related = $this->getRelation('highestPrice');
return ($related) ? $related->aggregate : null;
}
// now it's getting pretty simple
$products->first()->highestPrice; // highest price value we need
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…