0

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?

1
  • Might be a problem with Eloquent. does Task::whereRaw('due_at >= NOW()')->get() works? Commented Feb 12, 2015 at 6:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.