Auditable is a package that helps to trace the create, update and delete actions and when it's done on your Laravel Eloquent/Model.
Install the latest version using Composer.
1composer require pharaonic/laravel-auditable
You have to include Auditable in your migration and model first, then everything will be automated.
This is what you should to include in your migration file to create auditable columns, just use one of this 2 ways.
1// created_by, created_at2// updated_by, updated_at3$table->auditable(); 4 5// created_by, created_at6// updated_by, updated_at7// deleted_by, deleted_at8$table->auditableWithSoftDeletes();
This is what you should to include in your model file.
1namespace App\Models;2 3use Illuminate\Database\Eloquent\Model;4use Pharaonic\Laravel\Audits\Auditable; 5 6class Article extends Model7{8 use Auditable; 9}
This is how you can get the auditor who create/update/delete the record and when did it.
1// Creating 2$article = Article::create(['title' => 'Moamen Eltouny']); 3echo $article->created_at->isoFormat('LLLL'); 4echo $article->created_by->name; 5 6// Updating 7$article = Article::first(); 8echo $article->updated_at->isoFormat('LLLL'); 9echo $article->updated_by->name;10 11// Deleting (ONLY WITH SoftDeletes) 12$article = Article::first();13$article->delete();14echo $article->deleted_at->isoFormat('LLLL');15echo $article->deleted_by->name;