如何雄辩地更改groupBy之后的键值?

时间:2019-04-24 19:31:33

标签: php laravel laravel-5 eloquent laravel-collection

我按记录创建的月份对记录列表进行了分组:

$jobListings = JobListing::with('company')->orderBy('created_at')->get();

$jobListings = $jobListings->groupBy(function($j) {
    return Carbon::parse($j->created_at)->format('m');
});

return response()->json($jobListings);

这将为我提供一个JSON对象,该对象具有以数字格式按月键入的记录:

{
  "03": [...],
  "04": [...],
}

如何更改Eloquent集合的键,使月份读成“ March”和“ April”之类的字眼?

要清楚,我想输出的是:

{
  "March": [...],
  "April": [...],
}

This answer显示了如何将数字转换为月。

运行groupBy后如何更改此集合上的键?

1 个答案:

答案 0 :(得分:4)

您可以尝试使用format('F')

         return Carbon::parse($j->created_at)->format('F');
相关问题