好的..
我有一个“也许很简单”的要求。我有一个与配料模型和标签模型具有toMany关系的配方模型。
我需要一种方法来过滤出具有特定标签的配方(那里没有问题)..我还想过滤(排除)具有某些特定成分的配方。
$recipe = Recipe::whereHas('tags', function ($q) use ($tagId, $maxCal) {
$q->where('tag_key', $tagId);
})
->whereDoesntHave('ingredients', function ($q) use ($excludeIngredientGroups) {
$q->whereNotIn('ingredient_group_key', $excludeIngredientGroups);
})
$ excludeIngredientGroups是一个数组。
sql错误..:“ where子句”中的未知列“ ingredient.ingredient_group_key”
class Recipe extends Base
{
protected $table = "recipes";
public function ingredients()
{
return $this->hasMany('App\Models\RecipeIngredient', 'recipe_id', 'id')
}
class RecipeIngredient extends Base
{
protected $table = "recipes_ingredients";
public function ingredient(){
return $this->belongsTo('App\Models\Ingredient', 'ingredient_key', 'key');
}
public function recipe(){
return $this->belongsTo("\App\Models\Recipe","recipe_id","id");
}
是否有意义?
我认为您在食谱模型中的关系应该是:
1-成分:beforeToMany成分模型
2-配方成分有很多配方成分模型
class Recipe extends Base
{
protected $table = "recipes";
public function ingredients()
{
return $this->belongsToMany('App\Models\Ingredient', 'recipes_ingredients','recipe_id','ingredient_key');
}
public function recipeIngredients()
{
return $this->hasMany('App\Models\RecipeIngredient', 'recipe_id', 'id');
}
}
另一个说明:
-> whereDoesntHave('ingredients',函数($ q)使用($ excludeIngredientGroups){$ q-> whereNotIn('ingredient_group_key',$ excludeIngredientGroups);})
whereNotIn应该是:whereIn,因为您使用whereDoesntHave
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句