Laravel date not allowing me to use diffForHumans

Laravel date not allowing me to use diffForHumans

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

Stefan Bogdanescu

Founder & Senior Architect

Why diffForHumans() Fails on Your Timestamp Fields

You are encountering a specific runtime error: Call to a member function diffForHumans() on string. This happens because Laravel is treating your $created_at property as a plain string rather than an instance of the Carbon library. While you have correctly added 'created_at' and 'updated_at' to the $dates array in your Model, this configuration alone does not guarantee that the accessor returns a Carbon instance when accessed via dot notation on the model object.

In older versions of Laravel or under specific migration configurations, declaring fields in $dates tells the Eloquent mass assignment system to cast the incoming data to the correct format during create() and update() operations. However, it does not always trigger the automatic Carbon instance injection for existing records or when accessing them directly via $model->field. When you access $p->created_at, Laravel retrieves the raw value from the database (a string like "2023-10-27 14:30:00") and returns it as-is. Since strings do not have a diffForHumans() method, PHP throws the error you are seeing.

To resolve this, you need to ensure the attribute is explicitly cast to a Carbon instance in the model's $casts array. The $dates array was primarily designed for database hydration and mass assignment casting. By adding an explicit cast, you override the default behavior and force the accessor to return a CarbonInterface object regardless of how the data was stored or retrieved.

Update your Model definition as follows:

protected $casts = [
    'created_at' => 'datetime',
    'updated_at' => 'datetime',
];

Alternatively, you can keep the $dates array (which is still useful for ensuring mass assignment safety) and add the cast alongside it. This combination ensures robustness:

protected $dates = ['created_at', 'updated_at'];

protected $casts = [
    'created_at' => 'datetime',
    'updated_at' => 'datetime',
];

With this configuration, $p->created_at will return a Carbon instance, allowing you to chain methods like diffForHumans(), format(), or isToday() without issues.

The Trade-Off Between $dates and $casts

It is important to understand the distinction between these two arrays to avoid future confusion. Using $dates implies that the column contains a date/time value and that you intend to treat it as such throughout the application lifecycle, particularly when filling forms or using fill() methods. However, in modern Laravel versions (8.x and later), the recommended approach for accessing Carbon instances on model attributes is often to use the $casts array with the 'datetime' keyword.

If you are working with Laravel 10 or higher, there is also a class cast option. While 'datetime' works perfectly fine, using 'date' returns a plain string (which might be what you want if you only need formatting without Carbon methods), and 'timestamp' converts to an integer. The error you encountered stems from accessing the field as a string; switching to 'datetime' in $casts guarantees a Carbon instance is returned.

Performance Considerations

Some developers hesitate to add casts to every date column due to potential performance implications. While casting adds a tiny overhead of creating a new object instance, this is negligible compared to database queries and network I/O. However, if you have millions of records and are performing extremely tight loops where you only need the raw string (e.g., for immediate SQL formatting or specific legacy integrations), you might consider accessing the attribute via Attribute::get() without casting. For 99% of application logic, though, having a Carbon instance available makes your code significantly more readable and less error-prone.

If you are still experiencing issues after adding the cast, verify that your migration did not use the $timestamps property in a way that conflicts with manual casts, or check if you are using a custom accessor that returns a string. Once the explicit cast is in place, your diffForHumans() calls should work seamlessly across all models.

Our network