Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/HypathStack/model-scribe/llms.txt

Use this file to discover all available pages before exploring further.

ScribeLog is a standard Eloquent model that represents a single row in the audit log table. It ships with four query scopes for the most common filtering patterns, two morphTo relationships for loading the audited model and the actor who triggered the event, and a pair of convenience accessors for reading the before-and-after attribute state. You can use it anywhere you would use any other Eloquent model — in controllers, Livewire components, Nova resources, or Blade views.

Query Scopes

forSubject(Model $subject)
Constrains the query to entries whose subject_type and subject_id match the given model instance. Use this to retrieve the complete audit history for a specific record.
use HypathBel\ModelScribe\Models\ScribeLog;

$logs = ScribeLog::forSubject($order)->latest()->get();
forCauser(Model $causer)
Constrains the query to entries whose causer_type and causer_id match the given model instance. Use this to see every action a particular user has performed across the entire application.
$logs = ScribeLog::forCauser($user)->latest()->paginate(25);
forEvent(string $event)
Filters entries by the event column. The value should match one of the ScribeEvent enum string values: 'created', 'updated', 'deleted', 'restored', 'retrieved', or 'custom'.
$logs = ScribeLog::forSubject($invoice)
    ->forEvent('updated')
    ->latest()
    ->limit(10)
    ->get();
inLog(string $logName)
Filters entries by the log_name column. Use this when you have multiple named log stores (e.g. 'orders', 'invoices') and want to query only one of them.
$logs = ScribeLog::inLog('orders')->latest()->get();

Common Query Patterns

Scopes are chainable, so you can compose them to build precise queries:
use HypathBel\ModelScribe\Models\ScribeLog;

// All logs for a specific model instance
$logs = ScribeLog::forSubject($order)->latest()->get();

// All logs triggered by a specific user
$logs = ScribeLog::forCauser($user)->latest()->paginate(25);

// Combine scopes
$logs = ScribeLog::forSubject($invoice)
    ->forEvent('updated')
    ->latest()
    ->limit(10)
    ->get();

// Filter by log name (table routing)
$logs = ScribeLog::inLog('orders')->latest()->get();
Always eager-load the subject and causer relationships when iterating over a collection of log entries. Using ->with(['subject', 'causer']) prevents N+1 queries when you access $log->subject or $log->causer inside a loop or a Blade template.
$logs = ScribeLog::forCauser($user)
    ->with(['subject', 'causer'])
    ->latest()
    ->paginate(25);

Relationships

subject(): MorphTo
A polymorphic morphTo relationship that resolves the Eloquent model the log entry is about. The morph pair is stored in the subject_type and subject_id columns. The return type depends on the model that was audited — an Order, Invoice, or any other model using HasAuditLog.
causer(): MorphTo
A polymorphic morphTo relationship that resolves the Eloquent model that triggered the event. For observer-driven entries this is the authenticated user at the time of the event. For manual log entries it is whatever model you passed as the causer argument. May be null if no causer was present.

Casts

ScribeLog automatically casts two JSON columns to PHP arrays:
ColumnCastDescription
propertiesarrayThe full properties payload, including old and attributes keys for observer-driven entries
tagsarrayThe array of string tags attached to the entry

Attribute Accessors

Rather than digging into the properties array manually, ScribeLog provides two named accessors for the most commonly accessed keys.
$log->new_attributes
array
Returns $log->properties['attributes'] ?? [] — the state of the audited attributes after the event was applied. For created and restored events this is the full captured attribute set. For updated events it contains only the keys that changed.
$log->old_attributes
array
Returns $log->properties['old'] ?? [] — the state of the audited attributes before the event. For updated events this contains the previous values of every changed key. For deleted events it contains the model’s attributes at the time of deletion. For created events it is an empty array.

Displaying Logs in a Blade Template

The accessors and relationships make it straightforward to render a human-readable change history:
foreach ($logs as $log) {
    echo $log->event;       // 'updated'
    echo $log->log_name;    // 'orders'
    echo $log->causer->name; // 'Jane Smith'

    foreach ($log->old_attributes as $key => $oldValue) {
        $newValue = $log->new_attributes[$key];
        echo "{$key}: {$oldValue} → {$newValue}\n";
    }
}

Table Resolution

ScribeLog::getTable() resolves the table name from config('model-scribe.drivers.database.table'), falling back to 'model_scribe_logs' if the config key is absent. If you use a custom table for a named log store, query that table directly by calling ->setTable('invoices_logs') on a fresh model instance:
$logs = (new ScribeLog)->setTable('invoices_logs')
    ->forEvent('updated')
    ->latest()
    ->get();
Alternatively, subclass ScribeLog and override $table for each named store you want to query in a strongly-typed way.

Build docs developers (and LLMs) love