简体   繁体   English

使用日期过滤器的Laravel雄辩查询中的错误结果

[英]Wrong results in Laravel eloquent query using date filter

I have to extract results from DB having date column bigger than today (basically i need to see the up coming events). 我必须从具有比今天更大的date列的数据库中提取结果(基本上,我需要查看即将发生的事件)。

In real world I play in my MySql console: 在现实世界中,我在MySql控制台中玩:

select * from searcheable where MATCH (title,description) AGAINST ('rock' IN BOOLEAN MODE) and date > CURDATE() order by date  asc

And it works well. 而且效果很好。

I'm trying to extract same data in eloquent style using Laravel, and I wrote: 我试图使用Laravel以雄辩的方式提取相同的数据,我写道:

$results = Search::search($key)
            ->whereDate("date",">",' CURDATE()')
            ->orderBy("date",'asc')
            ->paginate();

But it returns wrong results having wrong date. 但是它返回错误的结果和错误的日期。

Note the search metohd I used is for a fulltext. 请注意,我使用的搜索方法是全文的。 I don't think it's the issue. 我不认为这是问题。

If I do a debug and I print the sql using dd($results) it return: 如果执行调试,并且使用dd($ results)打印sql,则会返回:

select * from `searcheable` where MATCH (title,description) AGAINST (? IN BOOLEAN MODE) and date(`date`) > ? order by `date` asc

which is very similar to the starting query I'm working on. 这与我正在处理的开始查询非常相似。

What's wrong in my eloquent query? 我雄辩的查询出了什么问题?

Thanks for your time :) 谢谢你的时间 :)

try like this 这样尝试

 ->whereDate("date",">", now());

now() will return a Carbon DateTime instance, Which will be automatically casted to appropriate format now()将返回Carbon DateTime实例,该实例将自动转换为适当的格式

As it looks probably your error is in the CURDATE() format, you need to be sure both dates are in the same format, for the currentdate I always use $currentdate = Carbon::now(); 看起来您的错误可能是CURDATE()格式,因此您需要确保两个日期都采用相同的格式,对于currentdate,我始终使用$currentdate = Carbon::now();

then use this date for your search... 然后使用这个日期进行搜索...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM