mercredi 24 avril 2019

How do I change the key values after a groupBy in eloquent?

I've grouped a list of records by the month they were created in:

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

This will give me a JSON object that has records keyed by month in numerical format:

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

How can I change the keys of this Eloquent collection so that the months read as words like "March" and "April"?

To be clear, what I'd like to output is:

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

This answer shows how to convert the numbers to months.

How do I change the keys on this collection after running the groupBy?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire