【问题标题】:Laravel Eloquent query builder combinationLaravel Eloquent 查询构建器组合
【发布时间】:2016-03-30 00:13:25
【问题描述】:

所以我需要找到所有ProductionTask 那个belongTo 某些Operation 如果

  • 在哪里'status', '<', 3
  • orWhere('expected_start', '>', $monday_date)

随着orWhere 的实现,与Operation 的雄辩关系中的operation_id 列将被忽略。

我该怎么办?

错误代码如下:

   return production\ProductionTask::where('operation_id', $operation->id)->where('status', '<', 3)->orWhere('expected_start', '>', $monday_date)->and('expected_end', '<', $sunday_date)->get();

【问题讨论】:

    标签: php laravel laravel-4 eloquent laravel-query-builder


    【解决方案1】:

    你需要使用:

    return production\ProductionTask::where('operation_id', $operation->id)
         ->where(function($q) use($monday_date) {
             $q->where('status', '<', 3)->orWhere('expected_start', '>', $monday_date);
         }->where('expected_end', '<', $sunday_date)->get();
    

    将您的where 条件分组。

    使用这个你会得到:

    SELECT * FROM production_tasks WHERE operation_id = ? AND (status < 3 OR expected_start > ?) AND expected_end < ?
    

    使用以前的方式你会得到这样的东西:

    SELECT * FROM production_tasks WHERE operation_id = ? AND status < 3 OR expected_start > ? AND expected_end < ?
    

    它等于:

    SELECT * FROM production_tasks WHERE (operation_id = ? AND status < 3) OR (expected_start > ? AND expected_end < ?)
    

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 2016-02-09
      • 1970-01-01
      • 2016-02-11
      • 2021-08-17
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多