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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel Auditor Audits Your App With Your Own AI Agent image

Laravel Auditor Audits Your App With Your Own AI Agent

Read article
A simple form builder that stays out of your way image

A simple form builder that stays out of your way

Read article
Laravel AI: Trace Agent Runs With Lifecycle Events image

Laravel AI: Trace Agent Runs With Lifecycle Events

Read article
Laravel AI: Get Raw HTTP Responses and Rate Limits image

Laravel AI: Get Raw HTTP Responses and Rate Limits

Read article
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article