I have two tables like this:
products:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Product 1 |
| 2 | Product 2 |
| 3 | Product 3 |
| 4 | Product 4 |
+----+-----------+
prices:
+----+-------+------------+---------------------+
| id | price | product_id | created_at |
+----+-------+------------+---------------------+
| 1 | 20 | 1 | 2014-06-21 16:00:00 |
| 2 | 10 | 1 | 2014-06-21 17:00:00 |
| 3 | 50 | 2 | 2014-06-21 18:00:00 |
| 4 | 40 | 2 | 2014-06-21 19:00:00 |
+----+-------+------------+---------------------+
I have this relationship on Product:
public function prices()
{
return $this->hasMany('Price');
}
I can easily run Product::with('prices')->get();
to get each product with each of the prices it's had.
How can I use Eloquent to only get the most recent price? (Also, what if I wanted the cheapest/most expensive price instead?)