user101289
user101289

Reputation: 10422

laravel 4 ORM query operators

I have a weird issue-- my Tasks table has a due_at column. I seeded the table with 50 values in the future, then in my admin controller I want to grab those tasks to show in the admin panel:

// AdminController.php
$tasks = Task::where('due_at', '>=', 'NOW()')->get();
dd($tasks); // shows zero results

Running the same query in phpmyadmin results in the expected 50 rows:

SELECT * FROM `tasks` WHERE due_at >= now(); // returns 50 rows

If I just reverse the operator, it returns 50 results:

// AdminController.php
$tasks = Task::where('due_at', '<=', 'NOW()')->get();
dd($tasks); // shows 50 results

Am I missing something obvious?

Upvotes: 0

Views: 84

Answers (1)

delatbabel
delatbabel

Reputation: 3687

I think you need to do this:

$tasks = Task::where('due_at', '<=', DB::raw('NOW()'))->get();

Turn on query logging in your MySQL server and I think you will find that the Laravel 4 query builder is interpreting your string 'NOW()' into something you don't expect. You have to wrap that in DB::raw() to get the raw string into the query.

Upvotes: 1

Related Questions