Laravel eloquent get model on date

我的未来我决定 提交于 2019-12-12 02:26:35

问题


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

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

What am I doing wrong?


回答1:


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

$date->startOfDay();
$date->endOfDay();

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

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

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.




回答2:


The problem is not Laravel itself here but Carbon.

When using the following code:

use Carbon\Carbon;
$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 Carbon\Carbon;
$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 Carbon\Carbon;
$date = new Carbon('2014-10-07');
$date2 = new Carbon('2014-10-07');

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

or

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

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



回答3:


In your query lefts the table name:

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

Look: http://laravel.com/docs/4.2/queries#selects



来源:https://stackoverflow.com/questions/26231836/laravel-eloquent-get-model-on-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!