mardi 29 mars 2016

Laravel Eloquent: Return a value which is a sum() of a hasMany() with a constraint on a hasManyThrough()

I have three tables: PurchaseOrders, PurchaseOrderDetails, and Inventory.

Each PurchaseOrder has many PurchaseOrderDetails. Each Inventory item has many PurchaseOrder "through" a match of the SKU on PurchaseOrderDetails.

If I have the SKU, I want to do a sum of all PurchaseOrderDetails.OnOrder WHERE the parent PurchaseOrder has a NULL PurchaseOrder.ClosedDate.

How can I add this to my model so I can call App\Inventory::first()->OnOrder? I can get all the PurchaseOrders using:

public function OnOrder() {
    return $this->hasManyThrough(PurchaseOrder::class, PurchaseOrderDetail::class, 'LocalSKU', 'PONumber')->whereNull('PurchaseOrders.DateClosed');
}

I'm not sure how to break that down just to the PurchaseOrderDetails and sum up the OnOrder only where the SKU matches my original Inventory item.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1REzjvp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire