简体   繁体   English

Laravel pluck, groupBy自定义日期格式

[英]Laravel pluck, groupBy custom date Format

Can't Format created_at when use pluck.使用 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();

I usually use the code below to group by date, but it doesn't work with pluck and give a strpos() error.我通常使用下面的代码按日期分组,但它不适用于 pluck 并给出 strpos() 错误。

         //->groupBy(function($val) {
         //    return Carbon::parse($val->created_at)->format('d/m');
         //})

Is there a way to format date created_at when using pluck?使用 pluck 时有没有办法格式化日期 created_at ?

Give this a whirl:试一试:

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();

The formatting for the date is in the DATE_FORMAT() function.日期格式在DATE_FORMAT() function 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM