带关系的多个wherehas语句

时间:2019-11-24 15:03:33

标签: php mysql laravel eloquent orm

我正在尝试查询,可以在两个关系上同时添加两个条件:

return $this->builder
  ->whereHas('products.comment', function ($query) {
    $query->where('name', "some comment name");
  })
  ->whereHas('products', function ($query) {
    $query->where('type', 'some product type');
  });

问题是它给我错误的结果。如果我想获得正确的结果,则需要像这样构成查询:

return $this->builder
  ->whereHas('products', function ($query) {
    $query
      ->where('type', 'some product type')
      ->whereHas('comment', function ($query) {
        $query->where('name', "some comment name");
      })
  });

现在它将正常工作。不过,我需要第一种情况,因为我想在两个单独的函数中使用它们,并在以后组合所有条件。这种差异从何而来?

0 个答案:

没有答案