In most applications, a person eventually asks: "Who changed this record?" To answer, the application must record the user for each change. Many teams write the same code for this in each project. They set created_by_id in one controller, and they forget it in a different form.

We wrote this code in many client projects. Now it is a package: Laravel Blame.

What the package does

Laravel Blame records the user who creates, updates and soft-deletes an Eloquent model. It uses model events. You do not set the user in your controllers.

  • It sets the creator one time, when you create the model.
  • It sets the last editor each time you update the model.
  • It records who soft-deleted the model. When you restore the model, it clears this value.
  • It gives a relation for each user, so you can eager-load the users.
  • It gives schema macros that add the columns and the foreign keys.

Install the package

composer require bernskiold/laravel-blame

Add the columns

Use the blameable() macro in your migration. The macro adds created_by_id and updated_by_id. Both columns can be null. When you delete a user, the foreign key sets the value to null.

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->blameable();
    $table->timestamps();
});

You can also add one column at a time with createdBy(), updatedBy() and deletedBy(). Each macro accepts a column name and a table name. The table name is useful when the users table is in a different database.

$table->createdBy('author_id', 'reporting.users');

Add the trait

Add the Blameable trait to the model. This trait records the creator and the last editor.

use Bernskiold\LaravelBlame\Concerns\Blameable;

class Post extends Model
{
    use Blameable;
}

Then use the relations:

$post->createdBy; // The user who created the post
$post->updatedBy; // The user who last updated the post

If you need only one of the two values, use TracksCreatedBy or TracksUpdatedBy.

The package sets the creator only when the column is empty. Thus, you can set a different creator yourself, for example in an import.

Record who deleted a model

Use TracksDeletedBy to record who soft-deleted a model. This trait works only together with the SoftDeletes trait of Laravel. After a hard delete, there is no row that can keep the value.

Blameable does not include this trait, because most models do not use soft deletes. Add it yourself when you need it:

use Bernskiold\LaravelBlame\Concerns\TracksDeletedBy;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes, TracksDeletedBy;
}

Queues, commands and imports

By default, the package uses auth()->id() to find the user. A queued job or a console command has no authenticated user. In these processes, tell the package which user to record:

use Bernskiold\LaravelBlame\Support\Blame;

Blame::resolveUserIdUsing(fn () => $importJob->triggeredByUserId);

When the package cannot find a user, it does not change updated_by_id. Thus, a background job does not remove the last known editor.

Custom column names

You can change the column names for all models in config/blame.php. To change them for one model, use constants:

class Post extends Model
{
    use Blameable;

    public const CREATED_BY_COLUMN = 'author_id';
    public const UPDATED_BY_COLUMN = 'editor_id';
}

A limit to know

The package uses Eloquent model events. Mass updates and mass deletes do not send these events. For example, Post::query()->update([...]) does not set the columns. In these queries, set the values yourself.

Get the package

Laravel Blame is open source, with the MIT license. The code and the full documentation are on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.