Laravel Packages

Who Changed That? Tracking User Modifications in Models with Userstamps

Published
Who Changed That? Tracking User Modifications in Models with Userstamps image

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.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article