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

Merged
merged 1 commit into from
May 27, 2024

Conversation

derekmd
Copy link
Contributor

@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
Contributor

@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
Contributor 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
Contributor

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

@derekmd
Copy link
Contributor Author

derekmd commented May 26, 2024

@erikn69 erikn69 merged commit fe0e33a into owen-it:master May 27, 2024
@derekmd derekmd deleted the feat/without-auditing-method branch May 27, 2024 14:55
@erikn69
Copy link
Contributor

erikn69 commented May 27, 2024

@derekmd hi, this give me test errors: https://github.com/owen-it/laravel-auditing/actions/runs/9256833548

$result = Article::withoutAuditing(function () {
    $this->assertTrue(Article::$auditingDisabled);
    $this->assertFalse(ArticleExcludes::$auditingDisabled); //here, is true instead of false, why?

erikn69 added a commit to erikn69/laravel-auditing that referenced this pull request May 27, 2024
@derekmd
Copy link
Contributor Author

derekmd commented May 27, 2024

@erikn69: a08c673#diff-a23c87e87a036f61cc6c88f12299ba20cfbbc59b4a825ee48689ee701aeace25L5 merged after I opened this PR changed test stub ArticleExcludes extends Article so the two classes now share the same static $auditingDisabled state.

@erikn69
Copy link
Contributor

erikn69 commented May 27, 2024

Thanks, I already noticed it, #937

erikn69 added a commit that referenced this pull request May 27, 2024
@erikn69
Copy link
Contributor

erikn69 commented May 27, 2024

maybe another idea,
globally disable audits on all models within the callback block, maybe it could be done with a second argument

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

4 participants