PHPackages                             always-open/laravel-model-auditlog - 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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. always-open/laravel-model-auditlog

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

always-open/laravel-model-auditlog
==================================

Tracks changes made to models and logs them to individual tables.

v9.3.1(3mo ago)644.0k↓25.9%2MITPHPPHP &gt;=8.2CI passing

Since Apr 5Pushed 3mo agoCompare

[ Source](https://github.com/always-open/laravel-model-auditlog)[ Packagist](https://packagist.org/packages/always-open/laravel-model-auditlog)[ Docs](https://github.com/always-open/laravel-model-auditlog)[ RSS](/packages/always-open-laravel-model-auditlog/feed)WikiDiscussions 9.x Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (35)Used By (0)

Laravel Model Auditlog
======================

[](#laravel-model-auditlog)

[![Latest Version on Packagist](https://camo.githubusercontent.com/710513ed88d95ad13eadb7b77a459a281331a7e5fba80c08bfdb1c1e7553d6f3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c776179732d6f70656e2f6c61726176656c2d6d6f64656c2d61756469746c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/always-open/laravel-model-auditlog)[![Build Status](https://camo.githubusercontent.com/bcf62b529fa398b8d53ecf44bd6f526534406551fc604f5947d26cf41b44abb3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f616c776179732d6f70656e2f6c61726176656c2d6d6f64656c2d61756469746c6f672f74657374733f7374796c653d666c61742d737175617265)](https://github.com/always-open/laravel-model-auditlog/actions?query=workflow%3Atests)[![Total Downloads](https://camo.githubusercontent.com/a63c69d0b69d0ba559a832e294f9ceebe7b31aaf23736ad264a7924c9d51ed4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c776179732d6f70656e2f6c61726176656c2d6d6f64656c2d61756469746c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/always-open/laravel-model-auditlog)[![Maintainability](https://camo.githubusercontent.com/d2e3a830d522c2dd83e97f1ee882207c4dc12a0cea1aa7a3f1463da5b2fa5d8e/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36376131366132653136653933386637333330342f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/always-open/laravel-model-auditlog/maintainability)

When modifying a model record, it is nice to have a log of the changes made and who made those changes. There are many packages around this already, but this one is different in that it logs those changes to individual tables for performance and supports real foreign keys.

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

[](#installation)

You can install the package via composer:

```
composer require always-open/laravel-model-auditlog
```

Configuration
-------------

[](#configuration)

```
php artisan vendor:publish --provider="\AlwaysOpen\AuditLog\AuditLogServiceProvider"
```

Running the above command will publish the config file.

Usage
-----

[](#usage)

After adding the proper fields to your table, add the trait to your model.

```
// User model
class User extends Model
{
    use \AlwaysOpen\AuditLog\Traits\AuditLoggable;
```

To generate an auditlog model / migration for your models, use the following command:

```
php artisan make:model-auditlog "\\App\\User"
```

Replace `\App\User` with your own model name. Model, namespace, and table options can be tweaked in the config file.

If you need to ignore specific fields on your model, extend the `getAuditLogIgnoredFields()` method and return an array of fields.

```
public function getAuditLogIgnoredFields() : array
{
    return ['posted_at'];
}
```

Using this functionality, you can add more custom logic around what should be logged. An example might be to not log the title changes of a post if the post has not been published yet.

```
public function getAuditLogIgnoredFields() : array
{
    if ($this->postHasBeenPublished()) {
        return ['title'];
    }

    return [];
}
```

### Working with Pivot Tables

[](#working-with-pivot-tables)

Audit log can also support changes on pivot models as well.

In this example we have a `posts` and `tags` table with a `post_tags` pivot table containing a `post_id` and `tag_id`.

Modify the audit log migration replacing the `subject_id` column to use the two pivot columns.

```
Schema::create('post_tag_auditlog', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedInteger('post_id')->index();
    $table->unsignedInteger('tag_id')->index();
    $table->unsignedTinyInteger('event_type')->index();
    $table->unsignedInteger('user_id')->nullable()->index();
    $table->string('field_name')->index();
    $table->text('field_value_old')->nullable();
    $table->text('field_value_new')->nullable();
    $table->timestamp('occurred_at')->index()->default('CURRENT_TIMESTAMP');
});
```

Create a model for the pivot table that extends Laravel's Pivot class. This class must use the `AuditLoggablePivot` trait and have a defined `$audit_loggable_keys` variable, which is used to map the pivot to the audit log table.

```
class PostTag extends Pivot
{
    use AuditLoggablePivot;

    /**
     * The array keys are the composite key in the audit log
     * table while the pivot table columns are the values.
     *
     * @var array
     */
    protected $audit_loggable_keys = [
        'post_id' => 'post_id',
        'tag_id'  => 'tag_id',
    ];
}
```

Side note: if a column shares the same name in the pivot and a column already in the audit log table (ex: `user_id`), change the name of the column in the audit log table (ex: `audit_user_id`) and define the relationship as `'audit_user_id' => 'user_id'`.

The two models that are joined by the pivot will need to be updated so that events fire on the pivot model. Currently, a third-party package is required because Laravel doesn't support pivot model events.

```
composer require fico7489/laravel-pivot
```

Have both models use the PivotEventTrait

```
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use PivotEventTrait;
```

Modify the belongsToMany join on both related models to include the using function along with the pivot model. In the Post model:

```
public function tags()
{
    return $this->belongsToMany(Tag::class)
        ->using(PostTag::class);
}
```

In the Tag model:

```
public function posts()
{
    return $this->belongsToMany(Post::class)
        ->using(PostTag::class);
}
```

When a pivot record is deleted through `detach` or `sync`, an audit log record for each of the keys (ex: `post_id` and `tag_id`) will be added to the audit log table. The `field_value_old` will be the id of the record and the `field_value_new` will be null. The records will have an event type of `PIVOT_DELETED` (id: 6).

If you need to pull the audit logs through the `auditLogs` relationship (ex: $post\_tag-&gt;auditLogs()-&gt;get()), support for composite keys is required.

```
composer require awobaz/compoships
```

Then use the trait on the pivot audit log model:

```
use Awobaz\Compoships\Compoships;
use AlwaysOpen\AuditLog\Models\BaseModel;

class PostTagAuditLog extends BaseModel
{
    use Compoships;
```

For a working example of pivots with the audit log, see `laravel-model-auditlog/tests/Fakes`, which contains working migrations and models.

Note: Both models must use the AuditLoggable trait (ex: Post and Tag) so that `$post->tags()->sync([...])` will work.

### Testing

[](#testing)

```
composer test
```

### Using Docker

[](#using-docker)

All assets are set up under the docker-compose.yml file. The first time you run the docker image you must build it with the following command:

```
docker-compose build
```

Then you can bring it up in the background using:

```
docker-compose up -d
```

And the image is aliased so you can access its command line via:

```
docker exec -it processes-stamp-app /bin/bash
```

From there you can run the tests within an isolated environment

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security-related issues, please email @tomschlick / @qschmick instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tom Schlick](https://github.com/tomschlick)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance79

Regular maintenance activity

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~80 days

Total

32

Last Release

109d ago

Major Versions

v4.1.0 → v5.0.02021-06-15

v5.1.0 → v6.0.02022-02-10

v6.1.0 → 7.x-dev2022-06-29

v7.0.0 → v8.0.02023-07-17

v8.0.1 → v9.0.02025-02-17

PHP version history (6 changes)v0.1.0PHP ^7.1

v3.0.0PHP ^7.2.5

v4.0.0PHP ^7.3

v4.1.0PHP ^7.3|^8.0

7.x-devPHP ^8.0.0|^8.1.0

v9.1.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/b116525a1bd1456044dc2edc80dd04a09806549a38945eb9b905b143a642c4bd?d=identicon)[tomschlick](/maintainers/tomschlick)

---

Top Contributors

[![tomschlick](https://avatars.githubusercontent.com/u/70184?v=4)](https://github.com/tomschlick "tomschlick (14 commits)")[![qschmick](https://avatars.githubusercontent.com/u/5342767?v=4)](https://github.com/qschmick "qschmick (10 commits)")[![bstanley-pec](https://avatars.githubusercontent.com/u/218614749?v=4)](https://github.com/bstanley-pec "bstanley-pec (3 commits)")[![lroggen](https://avatars.githubusercontent.com/u/6265423?v=4)](https://github.com/lroggen "lroggen (2 commits)")[![solflare](https://avatars.githubusercontent.com/u/8484449?v=4)](https://github.com/solflare "solflare (1 commits)")

---

Tags

auditaudit-logaudit-logginghacktoberfestsysloglaravelloggingauditlogalways-open

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/always-open-laravel-model-auditlog/health.svg)

```
[![Health](https://phpackages.com/badges/always-open-laravel-model-auditlog/health.svg)](https://phpackages.com/packages/always-open-laravel-model-auditlog)
```

###  Alternatives

[hosmelq/laravel-logsnag

Integrate the power of LogSnag's real-time event tracking into your Laravel application.

237.9k](/packages/hosmelq-laravel-logsnag)

PHPackages © 2026

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