好吧,我被困住了,真的需要帮助。
这是设置:
1)表格项目:ID,名称
2)表格状态:ID,标题
3)表item_status:id,item_id,status_id,created_at
项目模型具有以下关系:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}
现在,我想要在2015-10-19和2015-10-20之间创建的最新状态为“ 7”或“ 9”的商品
我最接近的是:
$items = Item::with('statuses')->whereHas('statuses', function ($q) {
$q->where('created_at','>=','2015-10-19');
$q->where('created_at','<','2015-10-20');
$q->whereIn('status_id',array('7','9'));
}
);
问题是这不能正常工作。它提供了在该日期范围内获得其中status_id之一的所有项。
因此,如果商品在2015-10-19的状态为“ 7”,例如在2015-10-22的状态为“ 11”,则结果将为结果。我只希望在该日期范围内具有最新(最新)状态为“ 7”或“ 9”的商品。
更新:
例如:表格item_status
item_id status_id created_at
1 1 2015-10-06
1 3 2015-10-07
2 6 2015-10-07
2 3 2015-10-08
2 5 2015-10-09
如果我将过滤器设置为:status_id = 3且日期范围为2015-10-06至2015-10-09:我只希望ITEM 1,因为给定期间的ITEM 2具有另一个较新的状态(5)
请帮忙!
n
ÿ
最佳答案
使用时间戳列时,需要添加用于关联的->withTimestamps()
函数
例如:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}