whereBetween不能按预期使用日期

时间:2018-08-21 05:35:16

标签: php laravel eloquent

我有这样的查询:

App\Model::where('some', 'condition')->orWhere('some', 'other_condition')->whereBetween('created_at', [request('from'), request('to')])->get();

为什么whereBetween子句在这里不起作用?而不是在提供的日期之间返回记录,而是返回所有记录。

以下是请求参数:

from: 2018-08-14
to:   2018-08-09

我想念什么?

2 个答案:

答案 0 :(得分:1)

我认为这是在做

version: '3'

services:
  a-service:
      build: .
      image: service-name
      container_name: container-name
      volumes:
        - /etc/localtime:/etc/localtime:ro

您可以运行DB :: enableQueryLog();在查询之前然后print_r(DB :: getQueryLog());  查看正在运行的实际查询。

可能想尝试

select from blah where some=condition or some=other_condition and created_at between date1 AND date2. 

我想会

App\Model::
  whereBetween('created_at', [request('from'), request('to')])
  ->where(function($query) { 
     $query
     ->where('some', 'condition')
     ->orWhere('some', 'other_condition')
   })->get();

答案 1 :(得分:1)

根据您的查询

App\Model::where('some', 'condition')->orWhere('some', 'other_condition')->whereBetween('created_at', [request('from'), request('to')])->get();

就像下面的查询

SELECT * FROM `table` WHERE `some` = 'text' OR `some` = 'other_text' AND `created_at` BETWEEN 'from' AND 'to'

因此,从该查询中,它将返回具有 some = text或some = other text

的所有数据

您需要进行如下查询

SELECT * FROM `table` WHERE (`some` = 'text' OR `some` = 'other_text') AND `created_at` BETWEEN 'from' AND 'to'

因此它将检查日期和文本之间

您应该尝试使用此

App\Model::whereRaw('some = condition OR some = other_condition')->whereBetween('created_at', [request('from'), request('to')])->get();