PHP code example of jeevanjoshi / laravel-audit-trail
1. Go to this page and download the library: Download jeevanjoshi/laravel-audit-trail 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/ */
jeevanjoshi / laravel-audit-trail example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Jeevanjoshi\LaravelAuditTrail\Traits\HasAuditTrail;
class User extends Model
{
use HasAuditTrail;
}
// Get all audit history for a specific record
$user->audits()->get();
// Get only updates
$user->audits()->ofAction('updated')->get();
// Get latest audit
$user->latestAudit();
// Check if model has any history
$user->hasAuditHistory();
// Get paginated history
$user->audits()->latest()->paginate(20);
// Revert a record to its previous state
$audit = $user->audits()->latest()->first();
$user->revertTo($audit->id);
// Returns true on success, false on failure
// This update will NOT be logged
$user->withoutAudit(function () use ($user) {
$user->update(['last_seen_at' => now()]);
});
class User extends Model
{
use HasAuditTrail;
// These fields will never be logged for this model
protected array $auditExclude = [
'two_factor_secret',
'last_seen_at',
];
}
return [
// Enable or disable the package entirely
'enabled' => env('AUDIT_TRAIL_ENABLED', true),
// Your user model
'user_model' => env('AUDIT_TRAIL_USER_MODEL', 'App\Models\User'),
// Dashboard URL prefix
// Default: your-app.com/audit-trail
'route_prefix' => env('AUDIT_TRAIL_ROUTE_PREFIX', 'audit-trail'),
// Middleware protecting the dashboard
'middleware' => ['web', 'auth'],
// Fields never logged across ALL models
'exclude_fields' => [
'password',
'remember_token',
'two_factor_secret',
'two_factor_recovery_codes',
'updated_at',
],
// Auto delete logs older than X days (null = keep forever)
'keep_for_days' => env('AUDIT_TRAIL_KEEP_DAYS', null),
// Records per page on dashboard
'per_page' => env('AUDIT_TRAIL_PER_PAGE', 20),
// Dashboard title
'dashboard_title' => env('AUDIT_TRAIL_TITLE', 'Audit Trail'),
];
// Filter by action
Audit::ofAction('created')->get();
Audit::ofAction('updated')->get();
Audit::ofAction('deleted')->get();
// Filter by model
Audit::forModel(User::class)->get();
// Filter by user
Audit::byUser($userId)->get();
// Filter by date
Audit::today()->get();
Audit::thisMonth()->get();