Laravel eloquent get model on date

I want to get all users created on a specified date:

// $date is a Carbon instance parsed from command line argument.
// I checked it and it is correct.

$users = User::where('created_at', '>', $date->startOfDay())
    ->where('created_at', '<', $date->endOfDay())

But this returns 0 results, whereas in the database there are rows that correspond to that date.

What am I doing wrong?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Carbon doesn’t behave like value object (ie. it’s not immutable), so this:


simply modifies the $date object and returns it back. That being said, the string that is passed to the query, is obtained when PDO binds it in the prepared statement, when $date is already mutated to endOfDay.

It means that you just pass reference to the object:

$start === $end; // true

So either use different objects:
$users = User::where('created_at', '>', $date->copy()->startOfDay())
  ->where('created_at', '<', $date->copy()->endOfDay())

or simply return the string you need in place, instead of the Carbon object:
$users = User::where('created_at', '>', $date->startOfDay()->toDateTimeString())
  ->where('created_at', '<', $date->endOfDay()->toDateTimeString())

still, $date will now hold xxxx-xx-xx 23:59:59 timestamp, so keep this in mind in case you need to work with this variable somewhere else.

Method 2

The problem is not Laravel itself here but Carbon.

When using the following code:

use CarbonCarbon;
$date = new Carbon('2014-10-07');

$start = $date->startOfDay();

$end = $date->endOfDay();

echo $start.' '.$end;

what you get is:
2014-10-07 23:59:59 2014-10-07 23:59:59

so Laravel will execute query:
select * from `users` where `created_at` >'2014-10-07 23:59:59' and `created_at` <'2014-10-07 23:59:59';

and obviously you will get no results.

As you see $start result is not what you expect here.

to make it work the solution I found is creating 2 carbon objects:

use CarbonCarbon;
$date = new Carbon('2014-10-07');
$date2 = new Carbon('2014-10-07');

$start = $date->startOfDay();

$end = $date2->endOfDay();

echo $start.' '.$end;

Now result is as expected:
2014-10-07 00:00:00 2014-10-07 23:59:59

Now you can use:
use CarbonCarbon;
$date = new Carbon('2014-10-07');
$date2 = new Carbon('2014-10-07');

$users = User::where('created_at', '>', $date->startOfDay())
    ->where('created_at', '<', $date2->endOfDay())

use CarbonCarbon;
$date = new Carbon('2014-10-07');
$date2 = new Carbon('2014-10-07');

$users = User::whereBetween('created_at', [$date->startOfDay(), $date2->endOfDay()])->get();

Method 3

In your query lefts the table name:

$users = DB::table('users')
                ->where('votes', '>', 100)
                ->orWhere('name', 'John')


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x