Answer the question
In order to leave comments, you need to log in
How to sort by one-to-many relationship?
How to sort by relationship?
The product has prices. The current price is selected based on the last one added. The price is stored as a separate Price object.
Product{
public function prices(){
return $this->hasMany(Price::class);
}
public function price(){
return $this->prices()->orderBy('created_at', 'desc')->first();
}
}
Answer the question
In order to leave comments, you need to log in
In Laravel, you can sort by several parameters.
User::orderBy('name', 'DESC')
->orderBy('email', 'ASC')
->get();
$price = Price::orderBy('created_at', 'desc')->first();
$products = $price->products;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question