Laravel pluck, groupBy 自定义日期格式
Laravel pluck, groupBy custom date Format
使用 pluck 时无法格式化 created_at。
VisitorsStatistic::where('landing_page_id', $this->landing_page->id)
->select('created_at', DB::raw('count(*) as visitors'))
->whereMonth('created_at', '>=', $this->start_month)
->whereMonth('created_at', '<=', $this->end_month)
->orderBy('created_at')
->groupBy('created_at')
->pluck('visitors','created_at')
->all();
我通常使用下面的代码按日期分组,但它不适用于 pluck 并给出 strpos() 错误。
//->groupBy(function($val) {
// return Carbon::parse($val->created_at)->format('d/m');
//})
有没有办法在使用 pluck 时格式化日期 created_at?
试一试:
VisitorsStatistic::where('landing_page_id', $this->landing_page->id)
->select(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') AS created_at"), \DB::raw('count(*) as visitors'))
->whereMonth('created_at', '>=', $this->start_month)
->whereMonth('created_at', '<=', $this->end_month)
->orderBy('created_at')
->groupBy('created_at')
->pluck('visitors','created_at')
->all();
日期格式在 DATE_FORMAT()
函数中。
使用 pluck 时无法格式化 created_at。
VisitorsStatistic::where('landing_page_id', $this->landing_page->id)
->select('created_at', DB::raw('count(*) as visitors'))
->whereMonth('created_at', '>=', $this->start_month)
->whereMonth('created_at', '<=', $this->end_month)
->orderBy('created_at')
->groupBy('created_at')
->pluck('visitors','created_at')
->all();
我通常使用下面的代码按日期分组,但它不适用于 pluck 并给出 strpos() 错误。
//->groupBy(function($val) {
// return Carbon::parse($val->created_at)->format('d/m');
//})
有没有办法在使用 pluck 时格式化日期 created_at?
试一试:
VisitorsStatistic::where('landing_page_id', $this->landing_page->id)
->select(\DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') AS created_at"), \DB::raw('count(*) as visitors'))
->whereMonth('created_at', '>=', $this->start_month)
->whereMonth('created_at', '<=', $this->end_month)
->orderBy('created_at')
->groupBy('created_at')
->pluck('visitors','created_at')
->all();
日期格式在 DATE_FORMAT()
函数中。