ModelScribe is a powerful, driver-based audit log package for Laravel. It allows you to effortlessly record every change in your Eloquent models and route them exactly where they need to go: a database table, a flat file, multiple targets simultaneously (stack), or custom targets like ELK or Webhooks.
Unlike other packages, ModelScribe excels at multi-table routing, allowing you to logically separate logs for different business domains into different database tables or even different connections.
- Eloquent Integration: Simple trait-based setup.
- Multi-Target Drivers: Support for
database,file, andstack(log to multiple places at once). - Multi-Table Routing: Map different models to different log tables or database connections.
- Deep Diffing: Records
oldvsnewattributes automatically. - Customizable Retention: Choose between
permanent,days-based, orrotating(keep N records). - Rich Context: Automatically captures URL, IP Address, User Agent, and the authenticated "causer".
- Batching: Group related operations with a unique
batch_uuid. - Developer Friendly: Clean API, Facades, and a prune command.
Install the package via composer:
composer require hypathbel/model-scribePublish the configuration and migrations:
php artisan vendor:publish --tag="model-scribe-config"
php artisan vendor:publish --tag="model-scribe-migrations"Run the migrations:
php artisan migrateThe config/model-scribe.php file allows you to define your drivers and their behavior.
You can define multiple "stores" within the database driver. This is perfect for high-traffic apps that want to keep order logs and invoice logs in separate tables.
'drivers' => [
'database' => [
'driver' => 'database',
'table' => 'model_scribe_logs', // Default table
'stores' => [
'invoices' => [
'table' => 'invoice_logs',
'connection' => 'audit_db', // Optional: use a different connection
],
'orders' => [
'table' => 'order_logs',
],
],
'retention' => [
'type' => 'days',
'days' => 90,
],
],
],Generate a migration for a new store:
php artisan model-scribe:make-table invoicesAdd the HasAuditLog trait to your Eloquent model.
use HypathBel\ModelScribe\Traits\HasAuditLog;
class Product extends Model
{
use HasAuditLog;
}By default, ModelScribe logs created, updated, and deleted. You can customize this per model.
class Order extends Model
{
use HasAuditLog;
// Only log specific events
protected array $auditEvents = ['created', 'updated'];
// Limit which attributes are recorded per event
protected array $auditAttributes = [
'updated' => ['status', 'total_price', 'shipping_address'],
];
// Route to a specific store/table
protected string $auditLogName = 'orders';
// Add searchable tags to every log entry
protected array $auditTags = ['warehouse-A', 'priority-high'];
}Sometimes you want to log actions that aren't tied to an Eloquent lifecycle.
use HypathBel\ModelScribe\Facades\ModelScribe;
use HypathBel\ModelScribe\Enums\ScribeEvent;
ModelScribe::log(
event: ScribeEvent::Custom,
logName: 'system',
description: 'User initiated a bulk export',
properties: ['format' => 'csv', 'rows' => 1200],
tags: ['export']
);Keep your log tables lean. ModelScribe includes a prune command that respects the retention policy defined in your config.
# Prune the default driver
php artisan model-scribe:prune
# Prune a specific driver
php artisan model-scribe:prune --driver=filecomposer testThe MIT License (MIT). See License File for more information.