Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withoutAuditing() callback method to temporarily disable auditing for one class #917

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

derekmd
Copy link

@derekmd derekmd commented Mar 21, 2024

Summary

Similar to Laravel's Model::withoutEvents() method, allow running Eloquent queries on a given model class inside a callback:

User::withoutAuditing(function () use ($user, $inputs, $additional) {
    // no audit
    $user->update($inputs);

    // this _will_ be audited if the Profile class is Auditable
    $user->profile()->updateOrCreate([], $additional);
});

Only the {class}::withoutAuditing() class will have auditing disabled, so additional model classes need their own call. e.g.,

User::withoutAuditing(fn () => $user->update($inputs));
Profile::withoutAuditing(fn () => $user->profile()->updateOrCreate([], $additional));

  1. It's wrapped in try / finally to handle restoring auditing after Eloquent possibly throws QueryException.
  2. If auditing is already disabled, that setting will be kept. i.e., don't call enableAuditing()

Links

Similar to Laravel's Model::withoutEvents() method,
allow running Eloquent queries on a given model class
inside a Closure.

User::withoutAuditing(function () use ($user, $inputs, $additional) {
    // no audit
    $user->update($inputs);

    // this _will_ be audited if the Profile class is Auditable
    $user->profiles()->updateOrCreate([], $additional);
});

Nest model class calls to disable auditing for many classes:

User::withoutAuditing(fn () => $user->update($inputs));
Profile::withoutAuditing(fn () => $user->profiles()->updateOrCreate([], $additional));
Copy link

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't hate this but I must ask, since this package makes use of Laravel events, could you not make use of ::withoutEvents() or ->saveQuietly() to achieve the same thing?

@derekmd
Copy link
Author

derekmd commented May 21, 2024

You can but that will also prevent other event listeners from being invoked. This method would allow only disabling audit callbacks.

@anteriovieira
Copy link
Member

Could someone please update the documentation? You could follow the idea from the Laravel documentation.

https://laravel.com/docs/11.x/eloquent#muting-events

@willpower232
Copy link

@derekmd would you like the honours? https://github.com/owen-it/laravel-auditing.com/ perhaps something under the advanced section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants