samedi 10 novembre 2018

How to obtain products when a relationship meets the Laravel Eloquent condition

I have the following relationship in the database, each table with its respective and related model.

enter image description here

I have products where each product has its recipe, which is composed of zero or more elements and the amount needed.

After this I have a warehouse table, where I have the current stock of the element, what I want is to obtain the products in which the amount needed in the recipe is less than the stock.

How could I obtain this data? it occurred to me to create a scope in the model products but I do not know how to perform the validation since they can be 0 or n elements in the recipe ?

Controller

Product::with('recipe')
            ->enableElements()->get();

Model Product

public function scopeEnableElements($query)
{

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire