Uncoke
Uncoke

Reputation: 1902

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).

In real world I play in my MySql console:

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:

$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:

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 :)

Upvotes: 0

Views: 1028

Answers (2)

Gustavo Jordan
Gustavo Jordan

Reputation: 49

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();

then use this date for your search...

Upvotes: 1

Shobi
Shobi

Reputation: 11461

try like this

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

now() will return a Carbon DateTime instance, Which will be automatically casted to appropriate format

Upvotes: 4

Related Questions