Skip to content

Deprecation error in HasAttributes::addDateAttributesToArray() when UPDATED_AT = null and model is cast to array (PHP 8.5) #57920

@Rodots

Description

@Rodots

Laravel Version

12.x

PHP Version

8.5.0

Database Driver & Version

MySQL9.5.0

Description

When using illuminate/database v12.4.1 with PHP 8.5, defining const UPDATED_AT = null; in an Eloquent model to disable the updated_at timestamp causes a deprecation error only when the model is converted to an array (e.g., via toArray() or (array) $model):

ErrorException: Using null as an array offset is deprecated, use an empty string instead
in vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php:254

This is triggered by the line:

if (! isset($attributes[$key])) {

inside addDateAttributesToArray(), where $key is null (from UPDATED_AT = null).

According to the PHP 8.5 release notes:

"Using null as an array offset or when calling array_key_exists() is now deprecated. Use an empty string instead."

Thus, isset($attributes[null]) is no longer allowed.

Suggested Fixes:

  1. Immediate fix: Skip null keys in addDateAttributesToArray():

    foreach ($this->getDates() as $key) {
        if ($key === null || ! isset($attributes[$key])) {
            continue;
        }
        // ...
    }
  2. Long-term API improvement: Consider deprecating the use of null to disable timestamp fields. Instead, align with the existing pattern used by $timestamps and allow:

    const UPDATED_AT = false;

    This would be more intuitive and consistent with public $timestamps = false;.

    The getDates() method could then filter out any timestamp field explicitly set to false (or null, for backward compatibility during a transition period).

This change would improve DX and avoid edge cases tied to PHP’s evolving handling of null as an array key.

Steps To Reproduce

  1. Define a model with:
    class MyModel extends Model
    {
        const UPDATED_AT = null;
        // ... other config (e.g., connection, table)
    }
  2. Fetch a record: $model = MyModel::first();
  3. Call $model->toArray(); (or cast to array).
  4. Observe the deprecation error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions