You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alternatively, you can still use the traditional event-driven approach if needed:
341
-
342
-
```php
343
-
<?php
344
-
345
-
declare(strict_types=1);
346
-
347
-
use Illuminate\Support\Facades\Event;
348
-
use iamfarhad\LaravelAuditLog\Events\ModelAudited;
349
-
350
-
$order = Order::find(1);
351
-
Event::dispatch(new ModelAudited(
352
-
model: $order,
353
-
action: 'status_changed',
354
-
oldValues: ['status' => 'pending'],
355
-
newValues: ['status' => 'shipped']
356
-
));
357
-
```
364
+
This approach provides better performance than event-driven architectures since it logs directly to the database without the overhead of event dispatching and listening.
These scopes allow for flexible and efficient querying of audit logs, making it easier to analyze changes based on action type, date range, or the user responsible for the change.
482
494
495
+
### Available Query Scopes
496
+
497
+
The `EloquentAuditLog` model provides several convenient scopes for filtering audit logs:
498
+
499
+
#### Entity and Basic Filtering
500
+
-`forEntity(string $entityClass)` - Filter by entity type (e.g., `User::class`)
501
+
-`forEntityId($entityId)` - Filter by entity ID
502
+
-`forAction(string $action)` - Filter by action (`created`, `updated`, `deleted`, etc.)
503
+
-`forCauser(string $causerClass)` - Filter by causer type
504
+
-`forCauserId($causerId)` - Filter by causer ID
505
+
506
+
#### Date Filtering
507
+
-`forCreatedAt($createdAt)` - Filter by exact creation date
508
+
-`dateGreaterThan($date)` - Filter for logs after a specific date
509
+
-`dateLessThan($date)` - Filter for logs before a specific date
510
+
-`dateBetween($startDate, $endDate)` - Filter for logs within a date range
511
+
512
+
#### Source Filtering
513
+
-`forSource(string $source)` - Filter by exact source match
514
+
-`fromConsole()` - Filter for logs from console commands
515
+
-`fromHttp()` - Filter for logs from HTTP requests
516
+
-`fromCommand(string $command)` - Filter by specific console command
517
+
-`fromController(string $controller = null)` - Filter by controller (or all controllers if no parameter)
518
+
519
+
```php
520
+
// Examples of scope combinations
521
+
use iamfarhad\LaravelAuditLog\Models\EloquentAuditLog;
522
+
523
+
// Complex filtering example
524
+
$logs = EloquentAuditLog::forEntity(User::class)
525
+
->forAction('updated')
526
+
->fromConsole()
527
+
->dateBetween(now()->subWeek(), now())
528
+
->forCauserId(1)
529
+
->orderBy('created_at', 'desc')
530
+
->paginate(20);
531
+
532
+
// Find all user updates from a specific command in the last 24 hours
0 commit comments