Description
- Laravel-mongodb Version: 3.9.2 (should still be present in 3.9.5)
- PHP Version: 8.1.17
- Database Driver & Version: 4.4
Description:
When UPDATED_AT
(or CREATED_AT
) is set to null
in the model, a warning is thrown when updating the model.
class SomeModel extends Model
{
public const UPDATED_AT = null;
}
Setting UPDATED_AT
to null
is a common way to allow for a created_at
column, but not updated_at
.
For example:
https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php#L62-L70
Here, Laravel checks that the updated_at column isn't null before setting it.
However:
https://github.com/jenssegers/laravel-mongodb/blob/v3.9.5/src/Eloquent/Model.php#L226-L230
Here, we get all the dates columns, which could include nulls. Then, calling Str::contains($key, '.')
results in a deprecation warning:
PHP Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/app/vendor/laravel/framework/src/Illuminate/Support/Str.php on line 242
Steps to reproduce
- Set
UPDATED_AT
tonull
in a model class. - Call
$model->update([/* ... */]);
- See deprecation warning
Expected behaviour
null should be skipped
Actual behaviour
Warning
Possible fix
foreach ($this->getDates() as $key) {
if ($key && Str::contains($key, '.') && Arr::has($attributes, $key)) {
Arr::set($attributes, $key, (string) $this->asDateTime(Arr::get($attributes, $key)));
}
}