Who Changed That? Tracking User Modifications in Models with Userstamps
Last updated on by Yannick Lyn Fatt
Laravel Userstamps is a package created by Matt McDonald which provides an Eloquent trait that automatically maintains created_by and updated_by columns on your model, populated by the currently authenticated user in your application.
Example
When defining your table migrations, you can use the userstamps() or userstampSoftDeletes() methods:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->string('slug')->unique(); $table->boolean('published')->default(false); // UserStamps columns $table->userstamps(); // or $table->userstampSoftDeletes(); $table->timestamps(); }); }};
A created_by, updated_by and deleted_by (if using SoftDeletes) columns will be added to your table.
You can now add the Userstamps trait to your model, and the user stamps will be automatically managed.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model;use Mattiverse\Userstamps\Traits\Userstamps; class Post extends Model{ use Userstamps; }
To install this package, use Composer:
composer require wildside/userstamps
Learn more and view the source code on GitHub.