Laravel 查询生成器 - 日期查询不起作用
Laravel Query Builder - Date Query not working
我正在使用 for loop
循环整个月
for ($i = 1; $i < $days; $i++) {
$date = now()->format('Y-m-') . $i; //result '2020-08-1'
$date = Carbon::parse($date); //parsing with carbon
$profit = Profit::whereDate('created_at', $date->format('Y-m-d'))->first(); //<-- this not working getting null data
$data[] = $profit;
}
但是这个工作
$profit = Profit::whereDate('created_at', '2020-08-01')->first(); //<-- its working fine statically
我做错了什么帮助我谢谢
这应该可行,我不得不相信我们漏掉了一块拼图。
话虽如此,在 Laravel 中,您无需费力地按日期查询:查询构建器将毫无问题地接受 Carbon 日期。所以我建议我们将您的代码简化为这个,看看它是否改变了什么:
$date = Carbon::today()->startOfMonth(); // This is the first day of today's month, i.e. 2020-08-01
for($i = 0; $i < $days; $i++) {
$profit = Profit::whereDate('created_at', $date)->first();
$data[] = $profit;
$date->addDay();
}
我正在使用 for loop
for ($i = 1; $i < $days; $i++) {
$date = now()->format('Y-m-') . $i; //result '2020-08-1'
$date = Carbon::parse($date); //parsing with carbon
$profit = Profit::whereDate('created_at', $date->format('Y-m-d'))->first(); //<-- this not working getting null data
$data[] = $profit;
}
但是这个工作
$profit = Profit::whereDate('created_at', '2020-08-01')->first(); //<-- its working fine statically
我做错了什么帮助我谢谢
这应该可行,我不得不相信我们漏掉了一块拼图。
话虽如此,在 Laravel 中,您无需费力地按日期查询:查询构建器将毫无问题地接受 Carbon 日期。所以我建议我们将您的代码简化为这个,看看它是否改变了什么:
$date = Carbon::today()->startOfMonth(); // This is the first day of today's month, i.e. 2020-08-01
for($i = 0; $i < $days; $i++) {
$profit = Profit::whereDate('created_at', $date)->first();
$data[] = $profit;
$date->addDay();
}