laravel carbon get day names from a date column from a collection

laravel carbon get day names from a date column from a collection

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Day Names from a Laravel Collection

When working with a collection of MyModel instances, you often need to transform raw date data into specific formats for analysis. A common requirement is extracting day names (e.g., "Monday", "Tuesday") from an event_date column to calculate frequency distributions or generate reports.

The primary issue in the scenario described stems from how the data is structured versus how it is being accessed. When you retrieve a collection using MyModel::all(), Laravel's Eloquent ORM automatically casts the event_date column into Carbon instances (assuming the column type is date or datetime). This is evident in your debug output, which shows Carbon objects rather than plain strings.

The error Undefined property: Illuminate\Support\Collection::$myDates occurs because of a structural misunderstanding in your loop logic. In your code snippet:

$filtered = collect([
    'myDates' => $collection->pluck('event_date'),
]);

You created a new collection containing a single key, myDates, which holds the plucked items. This structure results in $filtered having one item, where that item is itself a collection. Consequently, iterating over $filtered yields keys pointing to the outer collection, and attempting to access $item->myDates fails because the iteration variables do not map to the nested key you defined.

The Direct Mapping Approach

The most efficient way to extract day names from an existing collection of models is to apply the transformation directly to the primary collection without nesting it inside another collect() call. Since the event_date property is already a Carbon instance, you can access its methods immediately.

To get the list of day names as an array, map over the original collection:

$dayNames = $collection->map(function ($model) {
    return $model->event_date->locale('en_US')->format('l');
})->toArray();

Here, ->map() iterates through every model in $collection. The closure receives each $model instance. Accessing $model->event_date returns the Carbon object. The format('l') method retrieves the full localized day name (e.g., "Monday"). We explicitly set the locale to 'en_US' to ensure consistency, as default locales might vary based on server configuration. Finally, converting the result to an array makes it ready for further processing.

Handling Frequency Counts

Once you have the array of day names, counting frequencies is a standard operation. Laravel's collection methods make this trivial. You can group the items by their day name and count the occurrences:

$frequency = collect($dayNames)
    ->groupBy(fn ($name) => $name)
    ->map(function ($items, $name) {
        return [
            'day' => $name,
            'count' => count($items),
        ];
    })->toArray();

This approach uses groupBy to cluster identical day names. The subsequent map transforms the grouped structure into a clean array of associative arrays, each containing the day name and its frequency count. This data structure is ideal for passing to a frontend framework or exporting to CSV.

Performance Considerations

While mapping within the collection is concise, consider the scale of your dataset. If you are querying thousands of records solely to format strings, ensure that the query itself is optimized. The pluck method retrieves only the specified column, which is efficient, but avoid eager loading unnecessary relationships if they are not needed for this specific transformation.

If you prefer a raw SQL approach for massive datasets to avoid hydrating model instances just for formatting, you can use a raw query with Carbon's static methods:

$frequency = DB::table('events')
    ->selectRaw('DAYNAME(event_date) as day_name, COUNT(*) as count')
    ->groupBy('day_name')
    ->get()
    ->toArray();

This leverages the database engine to aggregate data, which is significantly faster than processing it in PHP memory. However, this requires MySQL (or a compatible driver) and returns strings directly from the database rather than Carbon instances.

Conclusion

The key takeaway is to maintain the integrity of your collection structure. Avoid nesting collections unless necessary for specific grouping operations, as it complicates iteration logic. By mapping over the primary collection and utilizing Carbon's format method with explicit locale settings, you can reliably extract day names and perform frequency analysis without encountering undefined property errors.

Our network