雄辩的关系加入“不在数组中的地方”过滤器

pkdkk

好的..

我有一个“也许很简单”的要求。我有一个与配料模型和标签模型具有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");
    }

是否有意义?

OMR

我认为您在食谱模型中的关系应该是:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel雄辩的过滤器对hasMany关系

来自分类Dev

Laravel雄辩的过滤器对hasMany关系

来自分类Dev

AngularJS过滤器检查值是否在数组中

来自分类Dev

雄辩的条件过滤器

来自分类Dev

雄辩的地方不在查询中?

来自分类Dev

PHP在数组过滤器后未从数组中打印MongoID

来自分类Dev

为什么这些rsync过滤器args在数组中传递时在bash中失败?

来自分类Dev

通过不在GROUP中的过滤器过滤SQL GROUP

来自分类Dev

javascript在数组过滤器内嵌套for循环

来自分类Dev

javascript在数组过滤器内嵌套for循环

来自分类Dev

数组中的Javascript嵌套过滤器

来自分类Dev

Python 中的 JSON 过滤器数组

来自分类Dev

Excel VBA中的过滤器数组

来自分类Dev

数组中的 lodash 过滤器

来自分类Dev

sqlalchemy与过滤器的关系

来自分类Dev

如何搜索不在过滤器中的JIRA问题

来自分类Dev

如何在SSRS中实施“不在”过滤器?

来自分类Dev

如何删除不在过滤器中的行

来自分类Dev

Laravel 雄辩的多表连接与过滤器

来自分类Dev

Ruby中带有额外过滤器的Activerecord关系

来自分类Dev

models.py中的Django过滤器关系

来自分类Dev

Django过滤器get_queryset中的ForeignKey关系

来自分类Dev

Ruby中带有额外过滤器的Activerecord关系

来自分类Dev

AngularJS过滤器null键:ngRepeat列表中的值行-数据存储在数组内的对象中

来自分类Dev

基本Vue JS搜索过滤器仅在数组中不区分大小写还是小写?

来自分类Dev

Scala Slick过滤器并加入

来自分类Dev

JSON数据数组中的AngularJS过滤器数组

来自分类Dev

JSON数据数组中的AngularJS过滤器数组

来自分类Dev

Javascript过滤器数组