Audit log
The audit log answers who did what, to what and when. Each record stores the action, the actor who did it, the subject it was done to, a few properties that explain it, and the time.
Larasell already records sign-ins, changes to admins and API tokens, and every time someone opens the audit log. You record the actions that matter to your store, such as refunds or price changes.
The app only ever adds records, never changes them. You record an action in the same database transaction as the change itself, so either both are stored or neither is.
Recording actions
Each kind of action is a class that extends AuditRecord. Create one with Artisan:
1php artisan make:larasell-audit-record Products/ProductPriceChangedThe command creates app/AuditRecords/Products/ProductPriceChanged.php. Fill in what the action
was done to and what explains it:
1namespace App\AuditRecords\Products;
2
3use App\Models\Product;
4use Larasell\Admin\Audit\AuditCategory;
5use Larasell\Admin\Audit\AuditRecord;
6use Larasell\Admin\Audit\Subject;
7
8class ProductPriceChanged extends AuditRecord
9{
10 public function __construct(
11 private readonly Product $product,
12 private readonly int $from,
13 private readonly int $to,
14 ) {}
15
16 public function event(): string
17 {
18 return 'product_price.changed';
19 }
20
21 public function category(): AuditCategory
22 {
23 return AuditCategory::BusinessCritical;
24 }
25
26 public function subject(): ?Subject
27 {
28 return new Subject('product', $this->product->id);
29 }
30
31 public function properties(): array
32 {
33 return ['from' => $this->from, 'to' => $this->to];
34 }
35}The event name is how records are stored and searched. The command creates it from the class
name, product_price.changed in this example. Never rename an event once you have recorded it.
Then record it with the Auditor inside the transaction of the change:
1use App\AuditRecords\Products\ProductPriceChanged;
2use Illuminate\Support\Facades\DB;
3use Larasell\Admin\Audit\Auditor;
4
5public function update(Product $product, int $price, Auditor $auditor): void
6{
7 DB::transaction(function () use ($product, $price, $auditor) {
8 $from = $product->price;
9
10 $product->update(['price' => $price]);
11
12 $auditor->record(new ProductPriceChanged($product, $from, $price));
13 });
14}When the record can't be stored, record throws an AuditFailed exception and the
transaction rolls back the change too. record refuses to run outside a transaction.
Records that change nothing
Some actions change nothing in the database, like a failed sign-in. Record those outside a
transaction by returning false from requiresTransaction:
1public function requiresTransaction(): bool
2{
3 return false;
4}Categories
The category decides how long a record is kept, see Retention.
AuditCategory::Securityfor sign-ins, admins, API tokens and other access to the store.AuditCategory::BusinessCriticalfor refunds, cancellations, price and stock changes.AuditCategory::AuditAccessfor reading and pruning the audit log itself.
Properties
Properties are stored as JSON and shown to other admins for years. Keep them to what explains the action, and keep personal data out of them where you can.
Properties must be plain values: strings, numbers, booleans, null or arrays of them. Format
dates and convert enums yourself. Larasell rejects properties whose name looks like a credential,
such as password, api_token or secret_hash, so they can't end up in the log by accident.
When you change the shape of properties, bump the record's version. Older records keep their
version, so you can still read them as they were stored:
1public function version(): int
2{
3 return 2;
4}Actors
You don't pass the actor on admin pages and API requests. Larasell records the signed-in admin, or the API token of an API request.
Jobs and commands have no one signed in, so they pass their actor. A SystemActor
is a job or command acting on its own, named for what it does:
1use Larasell\Admin\Audit\SystemActor;
2
3$auditor->record(new ProductPriceChanged($product, $from, $price), new SystemActor('price-import'));When the actor really can't be known, for example for data imported from your previous shop
system, pass an UnknownActor with the reason instead:
1use Larasell\Admin\Audit\UnknownActor;
2
3$auditor->record($record, new UnknownActor('Imported from the previous shop system'));Larasell throws an AuditFailed exception when a job or command records without an actor.
Custom actors
Anything else that acts in your store can be an actor, such as a customer or an integration.
Implement the Actor interface:
1namespace App\Integrations;
2
3use Larasell\Admin\Audit\Actor;
4
5class ErpIntegration implements Actor
6{
7 public function __construct(private readonly string $connection) {}
8
9 public function auditActorType(): string
10 {
11 return 'erp';
12 }
13
14 public function auditActorId(): string
15 {
16 return $this->connection;
17 }
18}The type is a stable name, not a class name, so records stay searchable when you move the
class. Pass the actor to record:
1$auditor->record(new ProductPriceChanged($product, $from, $price), new ErpIntegration('sap'));Always pass custom actors to record. Larasell only picks up admins and API tokens on its own.
Retention
By default, Larasell keeps every audit record forever. How long you need to keep them depends on your store and the law that applies to it.
Publish the config files to set a retention period:
1php artisan vendor:publish --tag=larasell-admin-configThen set the periods in config/larasell/admin/audit.php, in days:
1'retention' => [
2 'default_days' => 365,
3 'categories' => [
4 'security' => 730,
5 'audit_access' => null,
6 ],
7],A category you leave out keeps its records for default_days. A category set to null keeps its
records forever. The period counts from when the action happened.
Without publishing the config, set the default period in your .env file:
1LARASELL_ADMIN_AUDIT_RETENTION_DAYS=365Pruning records
Larasell deletes records older than their period when the larasell:prune-audit-log command
runs, and at no other time. Schedule it in routes/console.php:
1use Illuminate\Support\Facades\Schedule;
2
3Schedule::command('larasell:prune-audit-log')->daily();Each run adds an audit_log.pruned record with the periods it applied and how many records it
deleted. The audit log shows when its own retention settings changed.
The command fails without deleting anything when a period isn't a whole number of days above 0, or a category doesn't exist.
Legal hold
When you must keep every record, for example during a legal dispute, turn on the legal hold:
1LARASELL_ADMIN_AUDIT_LEGAL_HOLD=trueWhile it's on, larasell:prune-audit-log deletes nothing. It still records each run, so the audit
log shows the hold was in place.