tofiq/laravel-audit-trail

v0.1.0 2025-02-04 15:52 UTC

This package is auto-updated.

Last update: 2025-06-04 21:01:55 UTC


README

Laravel Audit Trail is a package that logs all SQL queries executed by your Laravel application, whether they originate from Eloquent Models or the Query Builder.

This package allows you to track query history effortlessly, requiring no modifications to your project beyond the initial setup. Once configured, all database interactions are automatically logged.

Installation

To install the package, run the following command via Composer.

composer require tofiq/laravel-audit-trail

After installation, publish the package configuration using:

php artisan vendor:publish --provider=Tofiq\\AuditTrail\\AuditTrailServiceProvider

This will generate a configuration file (config/audit-trail.php), where you can customize the package settings, also the necessary migration.

Then if you want to use the database trailer then run this as well:

php artisan migrate

Customizations

Using a Custom Eloquent Model

By default, Laravel Audit Trail stores logs in a database table. If you prefer to use a custom Eloquent model for audit logs, define it in the audit-trail.php configuration file:

<?php
return [
    ...

    'audit_model' => \App\Models\YourAuditTrailModel::class,
    ...
];

Ensure your custom model and migration are properly created and migrated.

Implementing a Custom Logging Service

If you need full control over how queries are logged (e.g., logging to a file, an external service, or a custom database structure), you can implement your own logging service.

  1. Create a custom service class implementing the AuditLoggerInterface:
<?php

namespace App\Services;

use Tofiq\AuditTrail\Contracts\AuditLoggerInterface;

class MyLogService implements AuditLoggerInterface
{
    public function log(string|null $tableName, string $operationType, string $query, array $bindings = [], int|float $time = 0): void
    {
        \Log::info('Query Logged', [
            'table' => $tableName,
            'operation' => $operationType,
            'query' => $query,
            'bindings' => $bindings,
            'execution_time' => $time,
        ]);
    }
}
  1. Register the custom service in a service provider:
    public function boot(): void
    {
        $this->app->bind(
            \Tofiq\AuditTrail\Contracts\AuditLoggerInterface::class,
            \App\Services\MyLogService::class
        );
    }

With this setup, Laravel Audit Trail will now use your custom logging service instead of the default database logger.

License

Laravel Audit Trail is open-sourced software licensed under the MIT license.