好的,我被困了,真的需要帮助。
这是设置:
1)表项:id,name
2)表状态:id,title
3)table item_status:id,item_id,status_id,created_at
项目模型有关系:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}
现在,我想要具有最新状态的物品' 7'或者' 9',在2015-10-19和2015-10-20之间创建 我最接近的是:
$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的所有项目。 因此,如果项目获得状态&#39; 7&#39;在2015-10-19,例如status&#39; 11&#39;在2015-10-22,它将在结果。我想只有最新(最新)状态&#39; 7&#39;或者&#39; 9&#39;在该日期范围内。
更新 例如:table 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)
请帮忙! TNX ÿ
答案 0 :(得分:0)
当您使用时间戳列
时,需要为关系添加->withTimestamps()
函数
例如:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}