php Laravel 雄辩,其中日期等于或大于 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44266337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel eloquent where date is equal or greater than DateTime
提问by Kaizokupuffball
I'm trying to fetch relational data from a model where the date column is higher or equal to the current time.
我正在尝试从日期列高于或等于当前时间的模型中获取关系数据。
The date column is formated as this: Y-m-d H:i:s
日期列的格式如下:Ymd H:i:s
What I'm trying to do is to grab all rows where the Y-m-d H:i:s is in the future.
我想要做的是获取 Ymd H:i:s 未来所在的所有行。
Example: lets assume the date is 2017-06-01 and the time is 09:00:00 Then i would like got all rows where the date is in the future, and the time is in the future.
示例:假设日期是 2017-06-01,时间是 09:00:00 然后我想得到日期在未来的所有行,时间在未来。
Currently my code looks like this, and it's almost working but it doesn't grab the rows where the date is the current day.
目前我的代码看起来像这样,它几乎可以工作,但它没有抓取日期为当天的行。
public function customerCardFollowups() {
return $this -> hasMany('App\CustomerCardFollowup', 'user_id') -> whereDate('date', '>', Carbon::now('Europe/Stockholm')) -> orderBy('date', 'asc') -> take(20);
}
What am I doing wrong? Thanks in advance.
我究竟做错了什么?提前致谢。
回答by Alexey Mezenin
Sounds like you need to use >=
, for example:
听起来您需要使用>=
,例如:
->whereDate('date', '>=', Carbon::now('Europe/Stockholm'))
回答by Bassil Qureshi
Here you can use this:
在这里你可以使用这个:
->where('date', '>=', date('Y-m-d'))
回答by Amin Shojaei
For Laravel's TestCases:
对于 Laravel 的测试用例:
$this->assertDatabaseHas('my_table', [
'name' => $name,
[ 'updated_at', '>=', $updatedAt ] // $updatedAt is a Carbon object
]);