我有这个函数,每月使用碳从MySQL获取数据
$calls = \DB::table('calls')
->where('owned_by_id', $report->id)
->where(\DB::raw('month(created_at)'), Carbon::today()->month)
->get();
$report->callsCount = $calls->count();
它的工作正常我想要做的是每周获取数据
我尝试修改这样的代码:
->where(\DB::raw('week(created_at)'), Carbon::today()->week)
但我在Laravel中遇到错误
未知的getter'week'
看你需要通过week
过滤日期,这意味着日期范围试试这个
$calls = \DB::table('calls')
->where('owned_by_id', $report->id)
->whereBetween('created_at', [Carbon::now()->subWeek()->format("Y-m-d H:i:s"), Carbon::now()])
->get();
$report->callsCount = $calls->count();
这意味着根据碳文档https://carbon.nesbot.com/docs/和laravel docs https://laravel.com/docs/5.8/queries#where-clauses获取过去7天到现在的所有数据
在两个碳日期实例之间使用whereBetween。
->whereBetween('created_at', [
Carbon\Carbon::parse('last monday')->startOfDay(),
Carbon\Carbon::parse('next friday')->endOfDay(),
])
就像@Abrar所说,如果你需要计算当前月份的总呼叫数,你可以使用whereBetween:
$calls = \DB::table('calls')
->where('owned_by_id', $report->id)
->whereBetween('created_at', [
now()->locale('en')->startOfWeek(),
now()->locale('en')->endOfWeek(),
])
->get();
$report->callsCount = $calls->count();
但是你想要计算每周你需要使用分组By的总呼叫数。祝好运!