Zombiesplat
Zombiesplat

Reputation: 953

Laravel returns empty result with a date parameter of 0

When I write a MySQL query and have a where clause like such.

SELECT *
FROM my_table
WHERE date_field > 0

It works just fine.

However when I try and translate that in to my Laravel

$query = $this->getQB();
$query->where('date_field', '>', 0);
$return = $query->get($cols);

I get an empty result set. I figured out I can change my where clause to say

$query->where('date_field', '!=', '0000-00-00 00:00:00');

and everything runs fine.

My question is: Why can't I do the greater than zero on a date field?

Upvotes: 0

Views: 577

Answers (1)

Jeroen Noten
Jeroen Noten

Reputation: 3624

I think (but I am not sure) that it is because Laravel converts 0 to a string '0'. Try this:

$query->where(DB::raw('date_field > 0'));

Edit: This might work too (not checked):

$query->where('date_field', '>', DB::raw('0'));

Edit 2: I looked it up, and I think I am right about the string conversion, see: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connection.php#L280

Here PDOStatement::execute() is executed, and in the PHP documentation it says:

All values are treated as PDO::PARAM_STR.

Upvotes: 2

Related Questions