PHPackages                             softartisan/laravel-model-audits - 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. softartisan/laravel-model-audits

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

softartisan/laravel-model-audits
================================

A lightweight and robust Laravel package to automatically audit and track model changes (Audit Log). It records old and new values, user responsible, IP address, and modification type (created, updated, deleted) via a simple Trait

v1.1.1(5mo ago)0349↓35.7%[1 PRs](https://github.com/softartisan-inc/laravel-model-audits/pulls)MITPHPPHP ^8.4CI passing

Since Dec 6Pushed 1mo agoCompare

[ Source](https://github.com/softartisan-inc/laravel-model-audits)[ Packagist](https://packagist.org/packages/softartisan/laravel-model-audits)[ Docs](https://github.com/softartisan-inc/laravel-model-audits)[ GitHub Sponsors](https://github.com/softartisan)[ RSS](/packages/softartisan-laravel-model-audits/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (14)Versions (6)Used By (0)

Laravel Model Audits
====================

[](#laravel-model-audits)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c1ed8b30d50ce2617cb75d3a2cdcead1515eae7c71fc3bd0171d665cca7d41b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f66746172746973616e2f6c61726176656c2d6d6f64656c2d6175646974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/softartisan/laravel-model-audits)[![Total Downloads](https://camo.githubusercontent.com/ae08ab9dbfdac987cdb66416cf6d3acc6a8041c7ec9a3e8ecd89573557566ecb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f66746172746973616e2f6c61726176656c2d6d6f64656c2d6175646974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/softartisan/laravel-model-audits)[![License](https://camo.githubusercontent.com/039285456ab2e424a215ad2f2a049ae45bb416caff523384eeb646e33fd0cc34/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736f66746172746973616e2f6c61726176656c2d6d6f64656c2d6175646974732e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Laravel Model Audits is a lightweight and robust package to automatically audit and track model changes. It records old and new values, the authenticated user, IP address, URL, user agent, and the event type (created, updated, deleted, restored) via a simple trait.

It automatically records:

- Who made the change (user ID)
- What happened (created, updated, deleted, restored)
- When it happened
- Where it came from (URL, IP address, user agent)
- Details: the exact old\_values and new\_values for modified attributes

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

[](#installation)

Install via Composer:

```
composer require softartisan/laravel-model-audits
```

Publish the config file (optional, recommended):

```
php artisan vendor:publish --tag="laravel-model-audits-config"
```

Publish the migration and run it:

```
php artisan vendor:publish --tag="laravel-model-audits-migrations"
php artisan migrate
```

Quick start: use the trait
--------------------------

[](#quick-start-use-the-trait)

Add the IsAuditable trait to any Eloquent model you want to audit:

```
use Illuminate\Database\Eloquent\Model;
use SoftArtisan\LaravelModelAudits\Concerns\IsAuditable;

class Post extends Model
{
    use IsAuditable;

    protected $fillable = ['title', 'content', 'secret_token'];
}
```

That's it. Creating, updating, or deleting this model will create audit rows.

Query audits for a model instance:

```
$post = Post::find(1);
$audits = $post->audits()->latest('audit_id')->get();
```

Mask sensitive fields
---------------------

[](#mask-sensitive-fields)

The package never logs attributes listed in the global\_hidden config. You can also hide per‑model fields by overriding getHiddenForAudit() or using the hidden\_for\_audit array property.

```
class Post extends Model
{
    use IsAuditable;

    protected array $hidden_for_audit = ['secret_token'];
}
```

Behavior with SoftDeletes and forceDelete
-----------------------------------------

[](#behavior-with-softdeletes-and-forcedelete)

- Soft delete (delete()) always records a "deleted" audit with old\_values
- forceDelete():
    - remove\_on\_delete=true (default) deletes all audits for the model
    - remove\_on\_delete=false keeps existing audits and records one extra "deleted" audit

Configuration highlights
------------------------

[](#configuration-highlights)

Edit config/model-audits.php to adjust behavior:

- audit\_on\_create, audit\_on\_update: toggle which events are recorded
- remove\_on\_delete: how to handle audits when a record is permanently deleted
- table\_fields: customize column names used by the audits table
- user.guards or user.resolver: control which user is linked to each audit row
- pruning: enable automatic cleanup of old audit rows

Helpful APIs
------------

[](#helpful-apis)

On your auditable models:

- audits(): MorphMany relation to ModelAudit
- getDiff(): returns \[attribute =&gt; \['old' =&gt; x, 'new' =&gt; y\]\] for a given audit
- restore(): applies the old\_values of a given audit back to the model (ignores removed columns)

Example of restoring using a specific audit entry:

```
$audit = $post->audits()->latest('audit_id')->first();
$audit->restore();
```

Pruning old audits
------------------

[](#pruning-old-audits)

Enable pruning in the config and schedule Laravel's prune command:

```
// config/model-audits.php
'pruning' => [
    'enabled' => true,
    'keep_for_days' => 90,
],
```

Then schedule pruning (app/Console/Kernel.php):

```
protected function schedule(Schedule $schedule): void
{
    $schedule->command('model:prune')->daily();
}
```

AI &amp; Model Context Protocol (MCP)
-------------------------------------

[](#ai--model-context-protocol-mcp)

This package provides an optional integration with Laravel MCP (Model Context Protocol) to let AI agents retrieve and analyze your model audits via a dedicated MCP server.

Optional installation:

1. Require MCP in your application (optional):

```
composer require laravel/mcp
```

2. Register the server (choose one or both transports):

```
use SoftArtisan\LaravelModelAudits\Integrations\Mcp\ModelAuditsServer;
use Illuminate\Support\Facades\Mcp;

// Web transport (HTTP)
Mcp::server('model-audits', ModelAuditsServer::class);

// Local transport (STDIO)
Mcp::local('model-audits', ModelAuditsServer::class);
```

What’s included:

- Tool: get-model-audit-history (SoftArtisan\\LaravelModelAudits\\Mcp\\Tools\\AuditHistoryTool)
    - Inputs: model\_class (string), model\_id (string|int), limit (int)
    - Returns a structured list of audits with compact diffs from `$audit->getDiff()`
- Prompt: analyze\_model\_changes (SoftArtisan\\LaravelModelAudits\\Mcp\\Prompts\\AuditAnalysisPrompt)
    - Guides the AI to call the tool and summarize/flag suspicious changes
- Server: ModelAuditsServer (SoftArtisan\\LaravelModelAudits\\Integrations\\Mcp\\ModelAuditsServer)
    - Exposes the above tool and prompt to clients via MCP

Notes:

- The MCP dependency is optional and declared under composer "suggest". Install `laravel/mcp` only if you want AI/MCP features.
- The tool supports both FQCN (e.g. `App\\Models\\Post`) or Laravel morph aliases as `model_class`.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security
--------

[](#security)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

License
-------

[](#license)

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

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance82

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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 ~1 days

Total

3

Last Release

154d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a999c3cc0aa856366bd17f1f8ae4173c739c8a56f9b74fc6a34e6063dda7818?d=identicon)[softartisan](/maintainers/softartisan)

---

Top Contributors

[![henoc35](https://avatars.githubusercontent.com/u/13746485?v=4)](https://github.com/henoc35 "henoc35 (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravelsoftartisanlaravel-model-audits

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/softartisan-laravel-model-audits/health.svg)

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

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

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

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[spatie/laravel-deleted-models

Automatically copy deleted records to a separate table

409109.8k4](/packages/spatie-laravel-deleted-models)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

203330.1k2](/packages/wnx-laravel-backup-restore)

PHPackages © 2026

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