lundi 10 avril 2017

Laravel 5.1 get Orders that have been completed

I have an Order model and an OrderStatus model. An Order can have multiple statuses but only one 'currentStatus' (this is defined by the created_at timestamp on the pivot table order_status).

Order model (orders table)

OrderStatus model (order_statuses table)

order_status pivot table (order_status table)

How can I define a query scope (or similar) to get all the Orders of a certain status; i.e. 'completed'?

The following doesn't work as it checks all statuses of the orders, even ones in the past.

public function scopeCompleted($query)
{
    return $query->whereHas('statuses', function ($q) {
        $q->where("order_statuses.status", 'complete');
    });
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire