PHPackages                             pierresilva/laravel-auditable - 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. pierresilva/laravel-auditable

ActiveLibrary

pierresilva/laravel-auditable
=============================

Audit for Eloquent Models.

039PHP

Since Apr 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/pierresilva/laravel-auditable)[ Packagist](https://packagist.org/packages/pierresilva/laravel-auditable)[ RSS](/packages/pierresilva-laravel-auditable/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Auditable
-----------------

[](#laravel-auditable)

Easily track any changes to an eloquent model.

### Installation

[](#installation)

```
composer require pierresilva/laravel-auditable

```

Insert the Revision service provider inside your `config/app.php` file:

```
pierresilva\Auditable\AuditableServiceProvider::class,
```

Run

`php arisan vendor:publish --"pierresilva\Auditable\AuditableServiceProvider"`

that publish the Auditable migration file.

Then, run `php artisan migrate`.

You're all set!

### Setup

[](#setup)

Insert the `pierresilva\Auditable\Traits\HasAuditsTrait` onto your model that you'd like to track changes on:

```
namespace App;

use pierresilva\Auditable\Traits\HasAuditsTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasAuditsTrait;

    /**
     * The morphMany revisions relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function audits()
    {
        return $this->morphMany(\pierresilva\Auditable\Models\Auditable::class, 'auditable');
    }

    /**
     * The current users ID for storage in revisions.
     *
     * @return int|string
     */
    public function auditUserId()
    {
        return auth()->id();
    }
}
```

### Usage

[](#usage)

###### Simple log

[](#simple-log)

```
\pierresilva\Auditable\Auditable::log('User try to log in.');
```

##### Get latest simple logs

[](#get-latest-simple-logs)

```
$logs = \pierresilva\Auditable\Auditable::latestSimpleLogs(100);
```

#### Audit Columns

[](#audit-columns)

You **must** insert the `$auditColumns` property on your model to track audits.

###### Tracking All Columns

[](#tracking-all-columns)

To track all changes on every column on the models database table, use an asterisk like so:

```
use pierresilva\Auditable\Traits\HasAuditsTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasAuditsTrait;

    /**
     * The columns to keep audits of.
     *
     * @var array
     */
    protected $auditColumns = ['*'];
}
```

###### Tracking Specific Columns

[](#tracking-specific-columns)

To track changes on specific columns, insert the column names you'd like to track like so:

```
class User extends Authenticatable
{
    use Notifiable, HasAuditsTrait;

    /**
     * The columns to keep audits of.
     *
     * @var array
     */
    protected $auditColumns = [
        'user_id',
        'title',
        'description',
    ];
}
```

#### Set message audit key

[](#set-message-audit-key)

To save a personalized message key do it as fallows

```
    $user = User::findOrFail($userId);
    $user->auditKey = 'User edited by ' . auth()->user()->name;
    $user->name = $request->get('username');
    $user->save();
```

#### Get Audits

[](#get-audits)

To get your audits on a record, call the relationship accessor `audits`. Remember, this is just a regular Laravel relationship, so you can eager load / lazy load your revisions as you please:

```
$user = User::with('audits')->find(1);

return view('user.show', ['user' => $user]);
```

On each audit record, you can use the following methods to display the revised data:

##### Get latest audits

[](#get-latest-audits)

```
$logs = \pierresilva\Auditable\Auditable::latestAudits(100);
```

###### getUserResponsible()

[](#getuserresponsible)

To retrieve the User that performed the revision, use the method `getUserResponsible()`:

```
$audit = Auditable::find(1);

$user = $audit->getUserResponsible(); // Returns user model

echo $user->id;
echo $user->email;
echo $user->first_name;
```

###### getOldValue()

[](#getoldvalue)

To retrieve the old value of the record, use the method `getOldValue()`:

```
$audit = Auditable::find(1);

echo $audit->getOldValue(); // Returns string
```

###### getNewValue()

[](#getnewvalue)

To retrieve the new value of the record, use the method `getNewValue()`:

```
$audit = Auditable::find(1);

echo $audit->getNewValue(); // Returns string
```

###### Example

[](#example)

```
// In your `post.show` view:

@if($post->revisions->count() > 0)

                User Responsible
                Message
                From
                To
                On

        @foreach($post->audits as $audit)

                    {{ $audit->getUserResponsible()->first_name }}

                    {{ $audit->getUserResponsible()->last_name }}

                {{ $audit->key() }}

                    @if(is_null($audit->old_value))
                        None
                    @else
                        {{ $audit->old_value }}
                    @endif

                {{ $audit->new_value }}

                {{ $audit->created_at }}

        @endforeach

@else
    There are no audits to show.
@endif
```

#### Modifying the display of column names

[](#modifying-the-display-of-column-names)

To change the display of your column name that has been revised, insert the property `$auditColumnsFormatted` on your model:

```
/**
 * The formatted revised column names.
 *
 * @var array
 */
protected $auditColumnsFormatted = [
    'user_id' => 'User',
    'title' => 'Post Title',
    'description' => 'Post Description',
];
```

#### Modifying the display of values

[](#modifying-the-display-of-values)

To change the display of your values that have been audited, insert the property `$auditColumnsMean`. You can use dot notation syntax to indicate relationship values. For example:

```
/**
 * The formatted revised column names.
 *
 * @var array
 */
protected $auditColumnsMean = [
    'user_id' => 'user.full_name',
];
```

You can even use laravel accessors with the `auditColumnsMean` property.

> **Note**: The audited value will be passed into the first parameter of the accessor.

```
protected $auditColumnsMean = [
    'status' => 'status_label',
];

public function getStatusLabelAttribute($status = null)
{
    if(! $status) {
        $status = $this->getAttribute('status');
    }

    return view('status.label', ['status' => $status])->render();
}
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6ce55dfbef0ea377fd37036b1d94a4657998999d15019542b3dd8938ca7ae72a?d=identicon)[pierresilva](/maintainers/pierresilva)

---

Top Contributors

[![pierresilva](https://avatars.githubusercontent.com/u/2513617?v=4)](https://github.com/pierresilva "pierresilva (6 commits)")

### Embed Badge

![Health badge](/badges/pierresilva-laravel-auditable/health.svg)

```
[![Health](https://phpackages.com/badges/pierresilva-laravel-auditable/health.svg)](https://phpackages.com/packages/pierresilva-laravel-auditable)
```

PHPackages © 2026

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