-
Notifications
You must be signed in to change notification settings - Fork 390
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
Conversation
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));
There was a problem hiding this 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?
You can but that will also prevent other event listeners from being invoked. This method would allow only disabling audit callbacks. |
Could someone please update the documentation? You could follow the idea from the Laravel documentation. |
@derekmd would you like the honours? https://github.com/owen-it/laravel-auditing.com/ perhaps something under the advanced section? |
@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: a08c673#diff-a23c87e87a036f61cc6c88f12299ba20cfbbc59b4a825ee48689ee701aeace25L5 merged after I opened this PR changed test stub |
Thanks, I already noticed it, #937 |
maybe another idea, |
Summary
Similar to Laravel's
Model::withoutEvents()
method, allow running Eloquent queries on a given model class inside a callback:Only the
{class}::withoutAuditing()
class will have auditing disabled, so additional model classes need their own call. e.g.,try
/finally
to handle restoring auditing after Eloquent possibly throwsQueryException
.enableAuditing()
Links