Effortless Record Auditing with Laravel Userstamps

Record changes in big applications can become complicated, particularly if several team members are updating the same database tables. In actual projects, companies tend to be interested in knowing who created, modified, or deleted a record for accountability and regulatory compliance. That is where record auditing using Laravel Userstamps can be a game saver.

Rather than doing it manually for user IDs or timestamps, Laravel Userstamps does this for you automatically. You can track changes to the model and tie them to authenticated users using the Laravel Userstamps. Let’s see how this can ease model auditing in Laravel 12 projects.

Why You Need Userstamps in Real Projects

Let’s keep using our example multi-author blog. Your editorial staff includes writers, editors, and administrators. A new product launch article is going to be published. Without userstamps, you are faced with a number of problems:

  • Accountability: If a writer posts an article containing mistakes of fact, you have no way of knowing who did it so that you can give them feedback.
  • Debugging: An editor sends word that their recent changes to a post have disappeared. By viewing the updated_by field, you can immediately determine if another user inadvertently overwrote their work.
  • Workflow Tracking: You can easily tell that a writer wrote the first draft and an editor was the last to make changes to it, ensuring it went through the proper workflow.

Installing Laravel Userstamps Package

To Integrate User stamps in Laravel, we will use danielemontecchi/laravel-userstamps package. It manage all the background logic for you and provide you ready to use methods. Open your terminal and enter below command to install laravel userstamps package:

composer require danielemontecchi/laravel-userstamps

After installation, publish the configuration file if needed:

php artisan vendor:publish --provider="DanieleMontecchi\Userstamps\UserstampsServiceProvider"

Using Laravel Userstamps in Your Application

Let’s see an example how to implement it into any model. It just work like soft delete functionality provided by Laravel itself. You just need to add database columns using migration and add it’s trait to into model then package will start it’s magic.

The package already provides two helper methods for migrations to add created_at, updated and deleted_at columns to database table.

$table->userstamps();
$table->softDeletesBy();

The userstamps() method adds created_at and updated_at column to the database schema and another one is for soft delete.

With these columns added, you can easily enable model auditing using Laravel Userstamps. Before moving forward you need to add an trait to the model which enables you to get user data for create, edit and delete operation using relationship. You just need to add the HasUserStamps trait to your models. With HasUserStamps, it will allow your model to work with this functionality.

<?php

use DanieleMontecchi\Userstamps\HasUserStamps;

class Post extends Model
{
    use HasUserStamps;
}

Now, your userstamps setup for post model is complete and once user perform some operations for posts it will log user’s data automatically into database. Which provide you methods to see which user has created record, who has edited some particular records and if you have implemented soft delete then you can see who has deleted record.

namespace App\Http\Controllers;

use App\Models\Post;

class PostController extends Controller
{
    public function show($id)
    {
        $post = Post::findOrFail($id);

        return response()->json([
            'title' => $post->title,
            'content' => $post->content,
            'creator' => $post->creator,
            'updater' => $post->updater,
            'destroyer' => $post->destroyer,
        ]);
    }
}

As you can see in above code, it’s getting post ID from URL and getting post details. With help of Laravel Userstamps it’s also returning creator, updater and destroyer records with response.

When user try to perform any operation for post eloquent model then package will store login user id as relevant value. It updates value in background using model events in Laravel. For adding this feature to every model by default, you can take similar approach from our post on “Adding Soft Delete to Every Model and Migration“.

Conclusion

Adopting record auditing with Laravel Userstamps not only increases transparency but also fosters trust within your system’s data. With the Laravel 12 Userstamps package integration, you give your application the ability to monitor changes.

Whether you’re building a CRM, ERP, or PMS, model auditing using Laravel 12 Userstamps ensures your records remain accurate, accountable, and easy to trace. It’s a simple yet powerful upgrade every Laravel developer should adopt.