---
title: Audit log
published: 2026-11-01
lastmod: 2026-11-01
url: https://larasell.dev/docs/admin/reference/audit-log
---

## Table of contents
- [Recording actions](#recording-actions)
  - [Records that change nothing](#records-that-change-nothing)
  - [Categories](#categories)
  - [Properties](#properties)
- [Actors](#actors)
  - [Custom actors](#custom-actors)
- [Retention](#retention)
  - [Pruning records](#pruning-records)
  - [Legal hold](#legal-hold)

# 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`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Audit/AuditRecord.php"}. Create one with Artisan:

```bash
php artisan make:larasell-audit-record Products/ProductPriceChanged
```

The command creates `app/AuditRecords/Products/ProductPriceChanged.php`. Fill in what the action
was done to and what explains it:

```php
namespace App\AuditRecords\Products;

use App\Models\Product;
use Larasell\Admin\Audit\AuditCategory;
use Larasell\Admin\Audit\AuditRecord;
use Larasell\Admin\Audit\Subject;

class ProductPriceChanged extends AuditRecord
{
    public function __construct(
        private readonly Product $product,
        private readonly int $from,
        private readonly int $to,
    ) {}

    public function event(): string
    {
        return 'product_price.changed';
    }

    public function category(): AuditCategory
    {
        return AuditCategory::BusinessCritical;
    }

    public function subject(): ?Subject
    {
        return new Subject('product', $this->product->id);
    }

    public function properties(): array
    {
        return ['from' => $this->from, 'to' => $this->to];
    }
}
```

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`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Audit/Auditor.php"} inside the transaction of the change:

```php
use App\AuditRecords\Products\ProductPriceChanged;
use Illuminate\Support\Facades\DB;
use Larasell\Admin\Audit\Auditor;

public function update(Product $product, int $price, Auditor $auditor): void
{
    DB::transaction(function () use ($product, $price, $auditor) {
        $from = $product->price;

        $product->update(['price' => $price]);

        $auditor->record(new ProductPriceChanged($product, $from, $price));
    });
}
```

When the record can't be stored, `record` throws an `AuditFailed`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Exceptions/AuditFailed.php"} 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`:

```php
public function requiresTransaction(): bool
{
    return false;
}
```

### Categories

The category decides how long a record is kept, see [Retention](#retention).

- `AuditCategory::Security` for sign-ins, admins, API tokens and other access to the store.
- `AuditCategory::BusinessCritical` for refunds, cancellations, price and stock changes.
- `AuditCategory::AuditAccess` for 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:

```php
public function version(): int
{
    return 2;
}
```

## 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`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Audit/SystemActor.php"}
is a job or command acting on its own, named for what it does:

```php
use Larasell\Admin\Audit\SystemActor;

$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`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Audit/UnknownActor.php"} with the reason instead:

```php
use Larasell\Admin\Audit\UnknownActor;

$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`{href="https://raw.githubusercontent.com/Larasell-dev/admin/refs/heads/main/src/Audit/Actor.php"} interface:

```php
namespace App\Integrations;

use Larasell\Admin\Audit\Actor;

class ErpIntegration implements Actor
{
    public function __construct(private readonly string $connection) {}

    public function auditActorType(): string
    {
        return 'erp';
    }

    public function auditActorId(): string
    {
        return $this->connection;
    }
}
```

The type is a stable name, not a class name, so records stay searchable when you move the
class. Pass the actor to `record`:

```php
$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:

```bash
php artisan vendor:publish --tag=larasell-admin-config
```

Then set the periods in `config/larasell/admin/audit.php`, in days:

```php
'retention' => [
    'default_days' => 365,
    'categories' => [
        'security' => 730,
        'audit_access' => null,
    ],
],
```

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:

```bash
LARASELL_ADMIN_AUDIT_RETENTION_DAYS=365
```

### Pruning 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`:

```php
use Illuminate\Support\Facades\Schedule;

Schedule::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:

```bash
LARASELL_ADMIN_AUDIT_LEGAL_HOLD=true
```

While it's on, `larasell:prune-audit-log` deletes nothing. It still records each run, so the audit
log shows the hold was in place.
