【问题标题】:Laravel 5 whereHas chaining and orWhereLaravel 5 whereHas 链接和 orWhere
【发布时间】:2015-04-09 13:31:08
【问题描述】:

尝试查询具有较低级别关系(协议)的模型,但无法进行链接工作。我尝试了下面的三种方法(第二种方法让我走了 99% 的路,但它错过了其中一个结果——非活动设备“状态”和“非活动”协议(应该被省略)。我是什么做错了吗?

每件设备都需要“有效”并具有有效的现有协议(有效协议是“有效”、“到期取消”或“未签署”的协议)。

    $thing = Equipment::wherehas('agreement', function($q)use($month)
    {
        $q->where('equipment.status', '=', 'Active')
          ->where('maintenance_months', 'like', '%'.$month.'%')
          ->where('status', '=', 'Active')
          ->orWhere('status', '=', 'Unsigned')
          ->orWhere('status', '=', 'Cancel on expiry');
    })->Get();

    $thing = Equipment::wherehas('agreement', function($q)use($month)
    {
        $q->where('status', '=', 'Active')
          ->orWhere(function($q2)use($month)
          {
                $q2->where('equipment.status', '=', 'Active')
                   ->where('maintenance_months', 'like', '%'.$month.'%');
          })
          ->where('status', '=', 'Unsigned')
          ->orWhere(function($q3)use($month)
          {
                $q3->where('equipment.status', '=', 'Active')
                   ->where('maintenance_months', 'like', '%'.$month.'%');
          })
          ->where('status', '=', 'Cancel on expiry')
          ->orWhere(function($q4)use($month)
          {
                $q4->where('equipment.status', '=', 'Active')
                   ->where('maintenance_months', 'like', '%'.$month.'%');
          });
    })->Get();      

    $thing = Equipment::wherehas('agreement', function($q)use($month)
    {
        $q->where('status', '=', 'Active')
          ->where('equipment.status', '=', 'Active')
          ->where('maintenance_months', 'like', '%'.$month.'%')
          ->orWhere('status', '=', 'Unsigned')
          ->where('equipment.status', '=', 'Active')
          ->where('maintenance_months', 'like', '%'.$month.'%')
          ->orWhere('status', '=', 'Cancel on expiry')
          ->where('equipment.status', '=', 'Active')
          ->where('maintenance_months', 'like', '%'.$month.'%');
    })->get();

【问题讨论】:

    标签: php mysql laravel eloquent laravel-5


    【解决方案1】:

    我不太明白为什么 where equipment.status 会一遍又一遍地过滤相同的东西,如果我没有理解错误,下面的代码可能是您正在寻找的结果。

    $thing = Equipment::whereHas('agreement', function($q)use($month)
    {
        $q->where('maintenance_months', 'like', '%'.$month.'%')
          ->whereIn('status',['Unsigned','Active','Cancel on Expiry'])
    })->where('status','Active')->get();
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2015-12-18
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多