skore-labs / laravel-auditable
Audit the users that performs actions to your application models
2.6.0
2024-05-24 11:40 UTC
Requires
- php: ^8.0
- illuminate/database: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- larastan/larastan: ^2.0
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0 || ^10.0
README
Audit the users that performs actions to your application models
Status
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
- Ruben Robles (@d8vjork)
- Skore (https://www.getskore.com/)
- And all the contributors