I have two tables. 1) products 2) prices
-------------------------
- products -
-------------------------
- id | int -
- name | varchar -
- created_at | datetime -
- updated_at | datetime -
-------------------------
----------------------------
- prices -
----------------------------
- id | int -
- product_id | int -
- sale_price | int -
- regular_price | int -
- created_at | datetime -
- updated_at | datetime -
-----------------------------
I want to search through products and get the last price of each product from prices table. I use this :
class Product extends Model
{
public function lastPrice()
{
return $this->hasOne(Price::class)->orderBy('id', 'DESC');
}
}
And I get products list with the last price using :
$products=Product::with('lastPrice')->paginate(9);
My question is: I want to order the result by highest/lower price. How I will do that?