owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.01k stars 387 forks source link

Warning explode(): Passing null to parameter #2 when using only created_at on model timestamps #832

Closed leandrodiogenes closed 1 year ago

leandrodiogenes commented 1 year ago
Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 9.52.2
Package version 13.5
PHP version 8.1.3

Actual Behaviour

I have a model that has the created_at column but doesn't have the updated_at column.

To continue using laravel timestamp only in created_at I set the constant UPDATED_AT to null in the model.

...
class MyModel extends Model implements \OwenIt\Auditing\Contracts\Auditable {

    use \OwenIt\Auditing\Auditable;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = null;
...
}

The problem is when I add the auditable to the model with timestamps=true in the auditable's config.

...
    /*
    |--------------------------------------------------------------------------
    | Audit Timestamps
    |--------------------------------------------------------------------------
    |
    | Should the created_at, updated_at and deleted_at timestamps be audited?
    |
    */

    'timestamps' => false,

im receiving an warning like that

<warning> DEPRECATED </warning> explode(): Passing null to parameter #2 ($string) of type string is deprecated in vendor\laravel\framework\src\Illuminate\Collections\Arr.php on line 281.

I found the problem here...

https://github.com/owen-it/laravel-auditing/blob/f7da93ef11a28a6dc656168d0479a37bd3843b59/src/Auditable.php#L114 The value of $this->excludedAttributes is:

[ 'created_at', 'null']

Expected Behaviour

An execution without any warning

Steps to Reproduce

Already mentioned

Possible Solutions

Filter the array of excluded attributes to avoid null values;

$attributes = Arr::except($this->attributes, array_filter($this->excludedAttributes));