-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
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:
-
Immediate fix: Skip
nullkeys inaddDateAttributesToArray():foreach ($this->getDates() as $key) { if ($key === null || ! isset($attributes[$key])) { continue; } // ... }
-
Long-term API improvement: Consider deprecating the use of
nullto disable timestamp fields. Instead, align with the existing pattern used by$timestampsand 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 tofalse(ornull, 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
- Define a model with:
class MyModel extends Model { const UPDATED_AT = null; // ... other config (e.g., connection, table) }
- Fetch a record:
$model = MyModel::first(); - Call
$model->toArray();(or cast to array). - Observe the deprecation error.