I have these relations:
Product Combo - Product (BelongsToMany)
Product - Inventory (BelongsToMany)
I have a quantity field in the pivot table between Product and Inventory to hold the product's quantity.
I want to get all the product combos that include products and those products have to be in stock (quantity > 0).
Here is my code:
/**
* Get available combos.
*
* @param $query
* @return mixed
*/
public function scopeAvailable($query)
{
return $query->where('status', 1)
->whereHas('products.inventories', function ($query) {
$query->where('quantity', '>', 0);
});
}
That code will work if all the products are out of stock (quantity = 0). If one of product in the combo has quantity > 0, it won't work anymore.
Thank you!
quantity > 0
? – Chin Leung