Php 其中未按预期使用日期
我有这样一个问题:Php 其中未按预期使用日期,php,laravel,eloquent,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
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
我错过了什么?我认为这是在做:
select from blah where some=condition or some=other_condition and created_at between date1 AND date2.
您可以运行DB::enableQueryLog();然后在查询之前打印(DB::getQueryLog());
查看正在运行的实际查询
我想试试
App\Model::
whereBetween('created_at', [request('from'), request('to')])
->where(function($query) {
$query
->where('some', 'condition')
->orWhere('some', 'other_condition')
})->get();
我想可以
select from blah where created_at between date1 AND date2 and (some=condition or some=other_condition).
我认为这是:
select from blah where some=condition or some=other_condition and created_at between date1 AND date2.
您可以运行DB::enableQueryLog();然后在查询之前打印(DB::getQueryLog());
查看正在运行的实际查询
我想试试
App\Model::
whereBetween('created_at', [request('from'), request('to')])
->where(function($query) {
$query
->where('some', 'condition')
->orWhere('some', 'other_condition')
})->get();
我想可以
select from blah where created_at between date1 AND date2 and (some=condition or some=other_condition).
如你所问
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();
如你所问
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();
接受@AlexandruPetrescu的回答。要感谢你的努力,就要向你的努力致敬。总有一天,你的方法也会有所帮助。但目前我不会采用whereRaw的
whereRaw
方法。接受@AlexandruPetrescu的答案。要感谢你的努力,就要向你的努力致敬。总有一天,你的方法也会有所帮助。但目前我不打算采用whereRaw方法。