Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Logging changes in Laravel using Traits
Emanuel Navarro
Emanuel Navarro

Posted on • Edited on

Logging changes in Laravel using Traits

In this post I'll show you how to track all the changes (created, updated and deleted) from any model that you want using laravel traits.

In Laravel, traits are a powerful tool for reusing code across multiple classes. They provide a way to share methods among classes without using traditional inheritance. Traits are like "partial classes" or mixins, allowing you to group related functionality together and avoid repetitive code.

First, let's create the migration for the table to record all the changes:

php artisan make:migration create_changelogs_table

// app/database/migrations/create_changelogs_table.phppublic function up(): void{    Schema::create('changelogs', function (Blueprint $table) {        $table->id();        $table->unsignedBigInteger('user_id')->nullable();        $table->string('action');        $table->string('model', 100);        $table->json('old_values')->nullable();        $table->json('new_values')->nullable();        $table->json('changed_values')->nullable();        $table->timestamps();        $table->foreign('user_id')->references('id')->on('users')            ->onDelete('NO ACTION')->onUpdate('NO ACTION');    });}public function down(): void    {    Schema:dropIfExists('changelogs');}
Enter fullscreen modeExit fullscreen mode

Then, we'll use the modelsevents to create the custom Observable trait:

// app/Traits/ObservableTrait.php<?phpnamespace App\Traits;use App\Models\ChangeLog;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Auth;trait ObservableTrait{    // The bootObservable() will be called on model instantiation automatically    public static function bootObservable()    {        static::saved(function (Model $model) {            if($model->wasRecentlyCreated) {                static::logChange($model, 'CREATED');            } else {                if(! $model->getChanges()) {                    return;                }                static::logChange($model, 'UPDATED');            }        });        static::deleted(function (Model $model) {            static::logChange($model, 'DELETED');        });    }    public static function logChange(Model $model, string $action)     {        ChangeLog::create([            'user_id' => Auth::check() ? Auth::user()->id : null,            'action'  => $action,            'model'   => $model::class,            'old_values' => $action !== 'CREATED' ? $model->getOriginal() : null,            'new_values' => $action !== 'DELETED' ? $model->getAttributes() : null,            'changed_values' =? $action == 'UPDATED' ? $model->getChanges() : null,        ]);    }}
Enter fullscreen modeExit fullscreen mode

Now you can use this trait in any model, just include the namespace and import it inside the model:

// app/Models/User.php<?phpnamespace App\Models;use App\Traits\ObservableTrait;class User extends Authenticatable{    use ObservableTrait;    //}
Enter fullscreen modeExit fullscreen mode

Every time you create, update or delete the model it will create a changelog record.

Note: If you want to log changes for specific cases, for example when a user log in, just use thelogChange method in the controller:

// app/Http/Controllers/Auth/LoginController.phppublic function authenticated(Request $request, User $user){    self::logChange($user, 'LOGGED');}

I hope you find this post helpful.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer | Passionate about new technologies 🚀
  • Location
    Argentina
  • Work
    Fullstack Developer @42mate
  • Joined

More fromEmanuel Navarro

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp