【发布时间】:2022-01-21 17:48:13
【问题描述】:
我有一个名为 products 的表,每次用户更新他们的产品时,都会在另一个名为 product_price_history 的表中保存一个新行。
我的桌子:
产品:
id (primary key)
name (varchar)
price (decimal)
product_price_history:
id (primary key)
product_id (fk to products)
price (decimal)
created_at (dateTime)
我的模特:
-
产品
-
产品价格历史
我在product model里面写了child table的关系如下
class Product extends Model {
public function history()
{
return $this->hasMany(ProductPriceHistory::class, 'product_id', 'id');
}
}
如何在 Laravel 中编写查询以显示价格下降的产品列表? 换句话说,显示最近降价的产品列表。
$result = Product::whereHas('history' , function($query){
// I do not know what to write here ...
})->get();
【问题讨论】:
-
对你来说“最近”是什么?
标签: php mysql laravel database laravel-8