与&WhereHas返回空的嵌套关系

时间:2019-12-11 22:08:56

标签: php laravel laravel-5 eloquent

我有以下口才查询,据我所知,该查询应返回与属性集相关的类别,而属性集又与属性相关,而属性又与变量相关。

$attributes = Category::with(['attributeSets.attributes.variants' => function ($query) {
            $query->where('id', 1)->withPivot('value');
        }])->whereHas('attributeSets.attributes.variants', function ($query) {
            $query->where('id', 1);
        })->get()->toArray();

但是,我收到以下内容,其中似乎包含与变量无关的属性(例如)“ variants” => []

如何删除这些属性?

array:9 [▼
  0 => array:12 [▼
    "id" => 7
    "parent_id" => null
    "category_type_id" => null
    "name" => "harum"
    "identifier" => "harum"
    "slug" => "harum"
    "sort_order" => 0
    "is_active" => 0
    "created_at" => "2019-12-11 15:45:51"
    "updated_at" => "2019-12-11 15:45:51"
    "parent" => null
    "attribute_sets" => array:1 [▼
      0 => array:8 [▼
        "id" => 1
        "name" => "ex"
        "identifier" => "ex"
        "created_at" => "2019-12-11 15:45:51"
        "updated_at" => "2019-12-11 15:45:51"
        "mainCategory" => array:12 [▶]
        "pivot" => array:2 [▶]
        "attributes" => array:3 [▼
          0 => array:8 [▼
            "id" => 1
            "attribute_set_id" => 1
            "name" => "minus"
            "identifier" => "minus"
            "created_at" => "2019-12-11 15:45:51"
            "updated_at" => "2019-12-11 15:45:51"
            "set" => array:6 [▶]
            "variants" => []
          ]
          1 => array:8 [▼
            "id" => 2
            "attribute_set_id" => 1
            "name" => "ducimus"
            "identifier" => "ducimus"
            "created_at" => "2019-12-11 15:45:51"
            "updated_at" => "2019-12-11 15:45:51"
            "set" => array:6 [▶]
            "variants" => []
          ]
          2 => array:8 [▼
            "id" => 3
            "attribute_set_id" => 1
            "name" => "mollitia"
            "identifier" => "mollitia"
            "created_at" => "2019-12-11 15:45:51"
            "updated_at" => "2019-12-11 15:45:51"
            "set" => array:6 [▶]
            "variants" => array:1 [▶]
          ]
        ]
      ]
    ]
  ]

我的模特:

类别

public function attributeSets()
    {
        return $this->belongsToMany(AttributeSet::class, 'attribute_set_categories');
    }

AttributeSet

public function attributes()
    {
        return $this->hasMany(Attribute::class);
    }

属性

public function variants()
    {
        return $this->belongsToMany(Variant::class, 'variant_attributes');
    }

0 个答案:

没有答案