kerattila/laravel-track-author

Extension to add created_by, updated_by and deleted_by columns to Eloquent models.

v2.0.1 2023-04-11 18:01 UTC

This package is auto-updated.

Last update: 2024-04-27 07:12:24 UTC


README

This package allow you to track the user who created, deleted and deleted eloquent modelss.

Install and configure

You can install the package via composer:

composer require kerattila/laravel-track-author

Publish the config file by running:

php artisan vendor:publish --provider="Kerattila\TrackAuthor\TrackAuthorServiceProvider"

After publishing configuration file, adjust the values accordingly your needs:

<?php

return [
    'models' => [
        'user' => \App\User::class
    ],
    'columns' => [
        'createdByColumnName' => 'created_by',
        'updatedByColumnName' => 'updated_by',
        'deletedByColumnName' => 'deleted_by',
    ]
];

Model and migration configuration

Migrations:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

Schema::table('posts', function (Blueprint $table) {
    // this will automatically add created_by, updated_by and updated_by nullable columns
    $table->trackAuthor();
});
            

Models

You can add the Kerattila\TrackAuthor\Traits\TrackAuthor trait to your models or just add one of the CreatedBy, UpdatedBy, DeletedBy traits.

Example:

<?php

namespace App;

// use Kerattila\TrackAuthor\Traits\CreatedBy;
// use Kerattila\TrackAuthor\Traits\UpdatedBy;
// use Kerattila\TrackAuthor\Traits\DeletedBy;
use Kerattila\TrackAuthor\Traits\TrackAuthor;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use TrackAuthor;
    // use CreatedBy, UpdatedBy, DeletedBy;
}

Relations

Relations are the type of BelongsTo and can be accessed as follows:

$post = \App\Post::find(1);
$createdBy = $post->createdBy;
$updatedBy = $post->updatedBy;
$deletedBy = $post->deletedBy;