PHP code example of softartisan / laravel-audit-events
1. Go to this page and download the library: Download softartisan/laravel-audit-events library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
softartisan / laravel-audit-events example snippets
// Before
use SoftArtisan\LaravelModelAudits\Concerns\IsAuditable;
config('model-audits.table_name');
// After
use SoftArtisan\LaravelAuditEvents\Concerns\IsAuditable;
config('audit-events.table_name');
use SoftArtisan\LaravelAuditEvents\Concerns\IsAuditable;
class Invoice extends Model
{
use IsAuditable;
}
$invoice->audits()->get(); // all audits for this model
$invoice->audits()->latest()->first(); // most recent audit
// Filtered by event type
$invoice->getCreatedHistory()->get();
$invoice->getUpdatedHistory()->get();
$invoice->getDeletedHistory()->get();
$invoice->getRestoredHistory()->get();
$invoice->getAuditHistory('invoice.sent')->get(); // any event name
// Diff between old and new values on an update audit
$audit = $invoice->getUpdatedHistory()->latest()->first();
$diff = $audit->getDiff();
// ['amount' => ['old' => 100, 'new' => 250]]
use SoftArtisan\LaravelAuditEvents\Models\ModelAudit;
// Filter by event name (CRUD or semantic).
ModelAudit::whereEvent('asset.status_changed')->get();
// Filter by a key inside the JSON `context` column (portable across
// MySQL / PostgreSQL / SQLite via Laravel's `->` JSON path operator).
ModelAudit::whereContext('mission_id', 42)->get();
// Filter by the anchored model instance (uses the indexed morph columns).
ModelAudit::forAuditable($invoice)->get();
// Compose them freely.
ModelAudit::forAuditable($asset)
->whereEvent('asset.status_changed')
->whereContext('mission_id', 42)
->latest('created_at')
->get();
$audit = $invoice->audits()->latest()->first();
$audit->restore(); // forceFills old_values back onto the model and saves
class Patient extends Model
{
use IsAuditable;
public function getHiddenForAudit(): array
{
return array_merge(parent::getHiddenForAudit(), [
'ssn',
'date_of_birth',
'medical_record_number',
]);
}
}
use SoftArtisan\LaravelAuditEvents\AuditContext;
class ImportInvoicesJob implements ShouldQueue
{
public function __construct(
private readonly int $userId,
private readonly string $batchId,
) {}
public function handle(): void
{
AuditContext::actingAs($this->userId, [
'source' => 'import-job',
'batch_id' => $this->batchId,
]);
// All audits produced here will carry $this->userId as causer
Invoice::create([...]);
Invoice::find(42)->update([...]);
AuditContext::reset(); // Always reset — prevents context bleed
}
}
public function saveHistory(
string $event,
array $oldValues = [],
array $newValues = [],
array $context = [],
): void
use SoftArtisan\LaravelAuditEvents\Concerns\IsAuditable;
use SoftArtisan\LaravelAuditEvents\Concerns\TracksRelationChanges;
class Role extends Model
{
use IsAuditable, TracksRelationChanges;
}
class RoleService
{
public function syncPermissions(Role $role, array $permissionIds): void
{
$before = $role->permissions->pluck('name')->all();
$role->permissions()->sync($permissionIds);
$after = $role->fresh()->permissions->pluck('name')->all();
$role->recordRelationAudit('permissions', $before, $after, [
'actor_id' => auth()->id(),
]);
}
}
$audit = Invoice::find(1)->audits()->latest()->first();
$audit->isSigned(); // true if the record has a non-null signature
$audit->verifySignature(); // true if the HMAC matches; false if tampered
'archive' => [
'enabled' => true,
'archive_after_days' => 90, // Records older than 90 days
'driver' => 'database', // 'database' | 'json_file'
'table_name' => 'audit_events_archive',
'path' => null, // Required for json_file driver; null = storage_path('audit-archives')
],
// bootstrap/app.php
use Illuminate\Console\Scheduling\Schedule;
->withSchedule(function (Schedule $schedule) {
$schedule->command('audit-events:archive')->weekly();
})