您可以查询Recipe
到WareHouse
由有关的element_id
。
配方型号:
public function warehouses()
{
return $this->hasMany(\App\Models\WareHouse::class, 'element_id', 'element_id');
}
仓库型号:
public function recipes()
{
return $this->hasMany(\App\Models\Recipe::class, 'element_id', 'element_id');
}
控制器代码:
Product::whereHas('recipes', function($q) {
$q->whereHas('warehouses', function($query) {
$query->whereRaw('WareHouse.stock > Recipe.count'); // use table names here, not model names
});
})->get();
如果您想使用 Recipe 模型中的范围来执行此操作:
public function scopeEnableElements($query)
{
$query->whereHas('warehouses', function($query2) {
$query2->whereRaw('WareHouse.stock > Recipe.count'); // use table names here, not model names
});
}
然后在您的控制器中:
Product::whereHas('recipes', function($query) {
$query->enableElements();
})->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句