Laravel Eloquent: How to use whereDate with Between?

南楼画角 提交于 2020-01-03 13:30:12

问题


I am using Laravel 5.5, I need to form a query where only date part of a datetime column should be matched, equivalent of date(date_col)='2018-01-01' kind of thing. How do I achieve this in Eloquent way? WhereDate() returns date part but is there some way to merge both?

Thanks


回答1:


For merging whereDate and between you can use something like this:

User::whereBetween(DB::raw('DATE(created_at)'), array($from_date, $to_date))->get();

sql :

select * from `users` where DATE(created_at) between '2018-02-01' and '2018-02-06'



回答2:


You can use Carbon:

$q->whereDate('date_col', '=', Carbon::parse($date)->toDateString());

whereDate should work too, pass in the value as the 2nd parameter

->whereDate('date_col', '2018-02-06')


来源:https://stackoverflow.com/questions/48657338/laravel-eloquent-how-to-use-wheredate-with-between

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