PHPackages                             esign/laravel-database-auditing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. esign/laravel-database-auditing

ActiveLibrary[Database &amp; ORM](/categories/database)

esign/laravel-database-auditing
===============================

Track database changes in Laravel using database triggers.

1.3.0(1mo ago)024MITPHPPHP ^8.2CI passing

Since Jul 10Pushed 1mo agoCompare

[ Source](https://github.com/esign/laravel-database-auditing)[ Packagist](https://packagist.org/packages/esign/laravel-database-auditing)[ Docs](https://github.com/esign/laravel-database-auditing)[ RSS](/packages/esign-laravel-database-auditing/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (14)Versions (7)Used By (0)

Track database changes in Laravel using database triggers.
==========================================================

[](#track-database-changes-in-laravel-using-database-triggers)

[![Latest Version on Packagist](https://camo.githubusercontent.com/43111be45da6c28e09e4b490638fd309218c03d7eab63cd4958128ca56a700b1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f657369676e2f6c61726176656c2d64617461626173652d6175646974696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/esign/laravel-database-auditing)[![Total Downloads](https://camo.githubusercontent.com/d170e66aca3b615d442fa8f9bc31f2e0b876bd1a460399cdeca02b4539803455/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f657369676e2f6c61726176656c2d64617461626173652d6175646974696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/esign/laravel-database-auditing)[![GitHub Actions](https://github.com/esign/laravel-database-auditing/actions/workflows/main.yml/badge.svg)](https://github.com/esign/laravel-database-auditing/actions/workflows/main.yml/badge.svg)

This package allows you to track changes in your database using database triggers. Currently only MySQL is supported.

> **Note**This package is designed to track database changes that occur outside of Laravel. However, for changes originating within a Laravel application, other solutions like [owen-it/laravel-auditing](https://github.com/owen-it/laravel-auditing) may better suit your requirements.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require esign/laravel-database-auditing
```

The package will automatically register a service provider.

This package comes with a migration to store your database changes. You can publish the migration file:

```
php artisan vendor:publish --provider="Esign\DatabaseAuditing\DatabaseAuditingServiceProvider" --tag="migrations"
```

Running this command will publish the following migration:

```
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('audits', function (Blueprint $table) {
            $table->id();
            $table->string('event');
            $table->morphs('auditable');
            $table->json('old_data')->nullable();
            $table->json('new_data')->nullable();
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('audits');
    }
};
```

Next up, you can optionally publish the configuration file:

```
php artisan vendor:publish --provider="Esign\DatabaseAuditing\DatabaseAuditingServiceProvider" --tag="config"
```

The config file will be published as config/database-auditing.php with the following contents:

```
return [
    /**
     * Specifies the model used by the package to retrieve audits.
     */
    'model' => Esign\DatabaseAuditing\Models\Audit::class,
];
```

Usage
-----

[](#usage)

### Creating database triggers

[](#creating-database-triggers)

To track database changes, use the `php artisan make:audit-trigger` command. This will generate a migration file with the necessary trigger configuration:

```
use Esign\DatabaseTrigger\DatabaseTrigger;
use Esign\DatabaseTrigger\Enums\TriggerEvent;
use Esign\DatabaseTrigger\Enums\TriggerTiming;
use Esign\DatabaseTrigger\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        Schema::createTrigger('audit_after_posts_insert', function (DatabaseTrigger $trigger) {
            $trigger->on('posts');
            $trigger->timing(TriggerTiming::AFTER);
            $trigger->event(TriggerEvent::INSERT);
            $trigger->statement("
                insert into audits (
                    event,
                    auditable_type,
                    auditable_id,
                    old_data,
                    new_data
                ) values (
                    'insert',
                    'post',
                    NEW.id,
                    NULL,
                    JSON_OBJECT('title', NEW.title, 'slug', NEW.slug)
                );
            ");
        });
    }

    public function down(): void
    {
        Schema::dropTriggerIfExists('audit_after_posts_insert');
    }
};
```

By default, a trigger name will be automatically assigned based on the provided input. However, you can specify a different name by passing it as the first argument:

```
php artisan make:audit-trigger my_trigger
```

### Retrieving tracked changes

[](#retrieving-tracked-changes)

After running the trigger migration, any modifications made to the associated table will be automatically monitored and stored in the `audits` table.

Here is a representation of how audits are stored in the `audits` table based on different trigger events:

ideventauditable\_typeauditable\_idold\_datanew\_data1insertpost1NULL{"title": "My Post"}2updatepost1{"title": "My Post"}{"title": "My Updated Post"}3deletepost1{"title": "My Post"}NULLTo retrieve the recorded changes in your Laravel project, you can utilize the `Esign\DatabaseAuditing\Models\Audit` model provided by the package. Here's an example:

```
use Esign\DatabaseAuditing\Models\Audit;

$audits = Audit::query()->get();
```

To determine if any data changes occurred in an audit, you can utilize the `hasDataChanges()` method available on the `Audit` model. Here's how you can use it:

```
use Esign\DatabaseAuditing\Models\Audit;

$latestAudit = Audit::latest()->first();
$latestAudit->hasDataChanges();
$latestAudit->hasDataChanges('slug');
```

The `hasDataChanges()` method returns a boolean value indicating whether any changes were made. If you pass a specific attribute name as an argument, it will check for changes in that particular attribute only.

To retrieve audits based on specific trigger events, you can use the `event()` scope provided by the `Audit` model. Here's an example:

```
use Esign\DatabaseAuditing\Models\Audit;
use Esign\DatabaseTrigger\Enums\TriggerEvent;

Audit::event(TriggerEvent::UPDATE)->first();
```

### Tracking changes in Eloquent models

[](#tracking-changes-in-eloquent-models)

To track changes related to your Eloquent model, apply the `Esign\DatabaseAuditing\Concerns\HasAudits` trait to the respective model. For instance:

```
use Esign\DatabaseAuditing\Concerns\HasAudits;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasAudits;
}
```

This will enable an `audits` relationship on the model, allowing you to access the tracked changes. For example:

```
$post = Post::first();
$latestAudit = $post->audits()->latest()->first();
```

Feel free to explore the [`Esign\DatabaseAuditing\Models\Audit`](./src/Models/Audit.php) model for more functionality related to tracking and retrieving changes.

### Testing

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance95

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 86.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~327 days

Total

4

Last Release

52d ago

PHP version history (2 changes)1.0.0PHP ^8.1

1.2.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4599d7a8f6fdb63dd04305a49ae5ec9700b7a6eacdbe3a54f89584d75e34503f?d=identicon)[esign](/maintainers/esign)

---

Top Contributors

[![jordyvanderhaegen](https://avatars.githubusercontent.com/u/24370626?v=4)](https://github.com/jordyvanderhaegen "jordyvanderhaegen (31 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (2 commits)")

---

Tags

esigndatabase-auditing

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/esign-laravel-database-auditing/health.svg)

```
[![Health](https://phpackages.com/badges/esign-laravel-database-auditing/health.svg)](https://phpackages.com/packages/esign-laravel-database-auditing)
```

###  Alternatives

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
