Disable Logging

Database seeds, bulk imports, and maintenance scripts create noise in your audit trail. AuditChain::withoutAudit() suppresses all audit logging within its callback.

Usage

use GrayMatter\AuditChain\Facades\AuditChain;

AuditChain::withoutAudit(function () {
    // No audit logs created during this callback
    User::factory()->count(1000)->create();
});

All Eloquent events and custom events are silently skipped while the callback executes.

Common Use Cases

Database Seeds

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        AuditChain::withoutAudit(function () {
            User::factory()->count(50)->create();
            Category::factory()->count(20)->create();
            Product::factory()->count(200)->create();
        });
    }
}

Bulk Imports

AuditChain::withoutAudit(function () use ($rows) {
    foreach ($rows as $row) {
        Product::updateOrCreate(
            ['sku' => $row['sku']],
            ['price' => $row['price'], 'name' => $row['name']],
        );
    }
});

Maintenance Scripts

// Fix a data inconsistency without cluttering the audit trail
AuditChain::withoutAudit(function () {
    User::whereNull('timezone')->update(['timezone' => 'UTC']);
});

Return Values

withoutAudit() returns whatever the callback returns:

$users = AuditChain::withoutAudit(function () {
    return User::factory()->count(10)->create();
});
// $users is the created collection

Nesting

Calls can be nested safely. Logging stays disabled until the outermost callback completes:

AuditChain::withoutAudit(function () {
    User::create([...]); // no audit log

    AuditChain::withoutAudit(function () {
        Post::create([...]); // no audit log
    });

    Comment::create([...]); // still no audit log
});

Important Notes

  • Custom events are also suppressed: $model->audit('shipped') inside withoutAudit() is a no-op.
  • Use sparingly: Every suppressed operation is a gap in your audit trail. For compliance-critical models, consider whether the gap is acceptable.
  • Consider context instead: If you want to keep the audit trail but mark entries as part of an import, use context instead of suppressing entirely.