【问题标题】:Eloquent query not returning the expected results from whereHas雄辩的查询没有从 whereHas 返回预期的结果
【发布时间】:2017-07-19 01:56:57
【问题描述】:

目前的关系:

  • 诊所有很多顾问
  • 顾问belongsToMany处理(数据透视表关系)

诊所可能没有顾问,顾问可能没有治疗。我只想返回has 顾问的诊所,其中has 治疗,其中包括与$treatment_id 匹配的治疗(因此has 存在检查)。

当前尝试(返回 0 个结果):

clinic::whereHas('consultants.treatments', function ($query) use ($treatment_id) {
                $query->where('treatment_id', $treatment_id);
            })
        ->paginate(10);

我觉得我需要一个whereHaswhereHas函数中,首先确定一个顾问是否有治疗,但是,如果这个逻辑是正确的,我该如何在函数中接近它。

我预计会返回 3 个结果,但目前是 0。

我怎样才能做到这一点?非常感谢,。

【问题讨论】:

    标签: laravel eloquent laravel-5.4


    【解决方案1】:

    既然是多对多,应该是:

    Clinic::whereHas('consultants.treatments', function ($query) use ($treatment_id) {
            $query->where('id', $treatment_id);
        })
        ->paginate(10);
    

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2014-11-27
      • 1970-01-01
      • 2015-02-14
      • 2017-03-15
      • 2018-04-13
      相关资源
      最近更新 更多