Ok, i'm stuck and really need help pls.
this is setup:
1) table items: id, name
2) table statuses: id, title
3) table item_status: id, item_id, status_id, created_at
item model has relation:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}
now, i want items with LATEST statuses '7' or '9', created between 2015-10-19 and 2015-10-20 closest i got is:
$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'));
}
);
Problem is that this is not working right. It gives all items that got one of these status_id's in that date range . So if item got status '7' on 2015-10-19, and for example status '11' on 2015-10-22, it will be in result. I would like to have only items with latest (newest) status '7' or '9' in that date range .
Update: for example: 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
if i set filter as: status_id = 3 and date range 2015-10-06 to 2015-10-09: i want only ITEM 1, because ITEM 2 in given period has another, newer status (5)
please help! tnx Y
You need to add ->withTimestamps()
function for relationship when your are using timestamp columns
Ex:
public function statuses(){
return $this->belongsToMany('Status')->withTimestamps();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.