8000 Having a null `updated_at` column throws an error when updating model · Issue #2533 · mongodb/laravel-mongodb · GitHub
[go: up one dir, main page]

Skip to content
Having a null updated_at column throws an error when updating model #2533
Closed
@goodevilgenius

Description

@goodevilgenius
  • 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

  1. Set UPDATED_AT to null in a model class.
  2. Call $model->update([/* ... */]);
  3. 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)));
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0