jeudi 22 octobre 2015

how to get last entry in given date range from pivot table

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')
}

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.

please help! tnx Y



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire