spatie / laravel-activitylog

Log activity inside your Laravel app
https://docs.spatie.be/laravel-activitylog
MIT License
5.36k stars 714 forks source link

Update logs empty nulls #1206

Closed boryn closed 1 year ago

boryn commented 1 year ago

Describe the bug

While logging activities on a model with soft deletes, having this setting (without logging created events):

protected static $recordEvents = ['updated', 'deleted', 'restored'];

and

public function getActivitylogOptions(): LogOptions
{
    return LogOptions::defaults()
                     ->logAll()
                     ->logOnlyDirty()
                     ->logExcept([
                         'updated_at',
                         'deleted_at',
                     ])
                     ->dontLogIfAttributesChangedOnly([
                         'is_ready',
                         'suggested_time',
                     ])
                     ->dontSubmitEmptyLogs();
}

there are nulls logged for the updated event like this:

{"old": {"id": null, "end": null, "start": null, "is_ready": null, "created_at": null}, "attributes": {"id": 20629138, "end": "2023-07-06", "is_ready": false, "created_at": "2023-07-02T07:29:41.000000Z"}}

It seems more like the created event being logged? Though I don't have it in the $recordEvents attribute and in the activity_log table these rows are logged with the updated event.

Expected behavior The "null" old values should not be logged at all for the updated event. Especially when there is null id.

Screenshots image

Versions (please complete the following information)

codeArtisanry commented 1 year ago

@boryn Your getActivitylogOptions() Helped me Thanks

sneakylenny commented 1 year ago

Something similar occurs to me, I have this:

Versions ``` PHP: 8.2.0 Laravel: v10.4.1 Package: 4.7.3 ```
Model ```php // Simplified version of my model use Spatie\Activitylog\LogOptions; use Spatie\Activitylog\Traits\LogsActivity; use App\Events; class Customer extends Model { use LogsActivity; // Included this as probable cause. protected $dispatchesEvents = [ 'created' => Events\CustomerCreated::class, 'updated' => Events\CustomerUpdated::class, 'deleted' => Events\CustomerDeleted::class, ]; protected $fillable = [ 'name', ]; public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logOnly(['name']); } // ... } ```
Controller ```php /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'name' => ['required', 'string', 'min:1', 'max:255', 'unique:customers,name'], ]); $customer = Customer::create([ 'name' => $validated['name'], ]); // (...) return redirect()->route('customers.index'); } ```

And when creating a customer it inserts this entry:

log_name description subject_id subject_type event causer_id causer_type properties batch_uuid
default updated 7 App\Models\Customer updated bb00215f-7b0f-4e7e-9d50-1672ab3b0fee App\Models\User\Types\TenantUser { "old": { "nam... null

With the properties column containing this:

{
    "old": {
        "name": null
    },
    "attributes": {
        "name": "The Coolest Company"
    }
}

While I have never updated this record, only created. Is this a bug?


Edit:

Adding this to my customer model poses no changes in the issue described above. Even though I'm still not updating but creating this model. So it appears related to this issue.

protected static $recordEvents = ['updated'];
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days