Laravel Query Builder - 日期现在使用碳的地方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34247140/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:48:40  来源:igfitidea点击:

Laravel Query Builder - Where date is now using carbon

laravellaravel-query-builder

提问by Reint

How to get only date on database column, I have difficulties using Carbon on controller:

如何仅获取数据库列上的日期,我在控制器上使用 Carbon 时遇到困难:

$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();

Date column looks like this in the database:

数据库中的日期列如下所示:

enter image description here

在此处输入图片说明

回答by Bogdan

That is a DATETIMEcolumn, so there's no need for additional formatting the Carboninstance. However you need to use whereDateif you want the fetch all users for which the datecolumn contains today's date:

那是一DATETIME列,因此不需要额外格式化Carbon实例。但是,whereDate如果您想要获取date列包含今天日期的所有用户,则需要使用:

$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();

Because when you pass Carbon::today()to the Query Builder method, the __toStringmethod will be automatically called and return a DATETIMEstring with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.

因为当你传递Carbon::today()给 Query Builder 方法时,该__toString方法会被自动调用并返回一个DATETIME格式为 from的字符串Carbon::DEFAULT_TO_STRING_FORMAT,这正是 MySQL 的格式Y-m-d H:i:s