Skip to content

Audit the users that performs actions to your application models

License

Notifications You must be signed in to change notification settings

skore/laravel-auditable

Repository files navigation

Laravel Auditable

Audit the users that performs actions to your application models

Status

packagist version tests StyleCI Codacy Badge Codacy Badge Scc Count Badge Scc Count Badge

Getting started

composer require skore-labs/laravel-auditable

And write this on the models you want to have auditables:

<?php

namespace SkoreLabs\LaravelAuditable\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use SkoreLabs\LaravelAuditable\Traits\Auditable;

class Post extends Model
{
    use Auditable;
    // Add this one and it will auto-detect for the deletedBy
    // use SoftDeletes;

    /**
     * The attributes that should be visible in serialization.
     *
     * @var array
     */
    protected $visible = ['title', 'content'];
}

And this is how it should look like in your migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTestTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('content');

            $table->timestamps();
            $table->softDeletes();
            $table->auditables();

            // or if has softDeletes (deleted_at column):
            $table->auditables(true);

            // also you might want to rename the users table:
            $table->auditables(true, 'my_users_table');
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Note: If you wanna remove it on the down method of your migrations, you can use dropAuditables.

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropAuditables();
            // or if has softDeletes (deleted_at column):
            $table->dropAuditables(true);
        });
    }

Methods

These are all the relationships that the Auditable trait provides to your model:

$post = Post::first();

$post->createdBy; // Author of the post
$post->updatedBy; // Author of the last update of the post
$post->deletedBy; // Author of the deletion of the post

$post->author; // Alias for createdBy

Support

This and all of our Laravel packages follows as much as possibly can the LTS support of Laravel.

Read more: https://laravel.com/docs/master/releases#support-policy

Credits