PHPackages                             orisintel/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. orisintel/laravel-model-auditlog

Abandoned → [always-open/laravel-model-auditlog](/?search=always-open%2Flaravel-model-auditlog)Library[Logging &amp; Monitoring](/categories/logging)

orisintel/laravel-model-auditlog
================================

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

v4.1.0(5y ago)2476.9k↓50%4[2 issues](https://github.com/orisintel/laravel-model-auditlog/issues)MITPHPPHP ^7.3|^8.0CI failing

Since Apr 5Pushed 5y ago4 watchersCompare

[ Source](https://github.com/orisintel/laravel-model-auditlog)[ Packagist](https://packagist.org/packages/orisintel/laravel-model-auditlog)[ Docs](https://github.com/orisintel/laravel-model-auditlog)[ RSS](/packages/orisintel-laravel-model-auditlog/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (9)Versions (16)Used By (0)

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

[](#laravel-model-auditlog)

[![Latest Version on Packagist](https://camo.githubusercontent.com/21ddbb7d1d45199493bc708ccc547a6292360e93b624d7af2bd40fbf35789709/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f726973696e74656c2f6c61726176656c2d6d6f64656c2d61756469746c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/orisintel/laravel-model-auditlog)[![Build Status](https://camo.githubusercontent.com/1d918ae8c4870dc00fb0ed5f348174a0bb70491bd7fb8fb25acb27a50d15d910/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6f726973696e74656c2f6c61726176656c2d6d6f64656c2d61756469746c6f672f74657374733f7374796c653d666c61742d737175617265)](https://github.com/orisintel/laravel-model-auditlog/actions?query=workflow%3Atests)[![Total Downloads](https://camo.githubusercontent.com/2fe9b3d2f81335c059e2f3aeec5d95a4965ff0ba5263642fd366fb23ad72b338/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f726973696e74656c2f6c61726176656c2d6d6f64656c2d61756469746c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/orisintel/laravel-model-auditlog)

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 orisintel/laravel-model-auditlog
```

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

[](#configuration)

```
php artisan vendor:publish --provider="\OrisIntel\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 \OrisIntel\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 / 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 that 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 Laravel doesn't support pivot events so a third party package is required.

```
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 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 OrisIntel\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  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

39

—

LowBetter than 85% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~48 days

Recently: every ~133 days

Total

15

Last Release

1912d ago

Major Versions

v0.1.3 → v1.0.42019-06-10

v1.2.0 → v2.0.02019-08-28

v2.0.1 → v3.0.02020-04-16

v3.0.0 → v4.0.02020-11-25

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

v3.0.0PHP ^7.2.5

v4.0.0PHP ^7.3

v4.1.0PHP ^7.3|^8.0

### 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 (10 commits)")[![qschmick](https://avatars.githubusercontent.com/u/5342767?v=4)](https://github.com/qschmick "qschmick (3 commits)")[![lroggen](https://avatars.githubusercontent.com/u/6265423?v=4)](https://github.com/lroggen "lroggen (2 commits)")

---

Tags

laravelloggingauditlogorisintel

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/orisintel-laravel-model-auditlog/health.svg)](https://phpackages.com/packages/orisintel-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)
