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
Laravel Query Builder - Where date is now using carbon
提问by Reint
回答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。


