我正在使用一个雄辩的查询来从表中检索数据.表格列如下所示: id started_at finished_at 1 2016-06-01 2016-06-30 2 2016-05-03 2016-05-28 我想要做的是,给定一个$date(例如:2016-06-18)并获取行的数据,即
id started_at finished_at 1 2016-06-01 2016-06-30 2 2016-05-03 2016-05-28
我想要做的是,给定一个$date(例如:2016-06-18)并获取行的数据,即from date和finished_at列之间的$date.
我在Laravel documentation中找到了whereBetween子句,但我没有想法正确使用它.
如果你可以使用Carbon,那么这段代码对我来说很好.$dateS = new Carbon('first day of January 2016'); $dateE = new Carbon('first day of November 2016'); $result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get();
或者您可以通过使用DB :: enableQueryLog()调试查询来检查问题;和DB :: getQueryLog();功能如
DB::enableQueryLog(); $dateS = new Carbon('first day of January 2016'); $dateE = new Carbon('first day of November 2016'); $result = ModelName::whereBetween('created_at', [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"])->get(); var_dump($result, DB::getQueryLog());