[英]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())
->get();
But this returns 0 results, whereas in the database there are rows that correspond to that date. 但这将返回0个结果,而在数据库中有对应于该日期的行。
What am I doing wrong? 我究竟做错了什么?
Carbon
doesn't behave like value object (ie. it's not immutable), so this: Carbon
行为不像值对象(即,它不是一成不变的),因此:
$date->startOfDay();
$date->endOfDay();
simply modifies the $date
object and returns it back. 只需修改
$date
对象并将其返回。 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
. 就是说,当
$date
已经突变为endOfDay
时,当PDO
在准备好的语句中将其绑定时,将获得传递给查询的字符串。
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: 或简单地返回您需要的字符串,而不是
Carbon
对象:
$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. 不过,
$date
现在将保留xxxx-xx-xx 23:59:59
时间戳,因此请记住这一点,以防您需要在其他地方使用此变量。
The problem is not Laravel itself here but Carbon. 问题不在于Laravel本身,而是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: 所以Laravel将执行查询:
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. 如您所见,
$start
结果不是您期望的结果。
to make it work the solution I found is creating 2 carbon objects: 为了使它起作用,我发现的解决方案是创建2个碳对象:
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();
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 外观: http : //laravel.com/docs/4.2/queries#selects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.