sohelamin / tracker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Model Tracker

Latest Version on Packagist Software License Total Downloads

By default, Eloquent automatically manage created_at and updated_at columns to exist on your tables. In addition to this it also manage a deleted_at attribute if the model use "soft delete". However it is very tedious job to manage who create/update the model as well as delete (if soft delete). This also a indication of code repetition.

If you wish to have these automatically managed by Eloquent, Tracker is a nice and convenient way to do this.

Install

You may use Composer to install the package into your Laravel project:

composer require phpcollective/tracker
Laravel 5.5+:

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

PhpCollective\Tracker\TrackingServiceProvider::class,

Database: Migrations

To add created_by and updated_by columns in your table you want to track just use $table->track() method in your table migration file. For dropping the columns use $table->dropTrack().

Schema::create('table', function (Blueprint $table) {
    ...
    $table->track();
});

// To drop columns
Schema::table('table', function (Blueprint $table) {
    $table->dropTrack();
});

If Your table contains soft delete columns, just pass boolean true in the method. It will add a deleted_by column in database.

Schema::create('table', function (Blueprint $table) {
    ...
    $table->softDeletes();
    $table->track(true);
});

// To drop columns with soft delete
Schema::table('table', function (Blueprint $table) {
    $table->dropTrack(true);
});

Model

Add Trackable trait in model you want to track. Now it will handle all CRUD event by the authenticate user.

<?php

namespace App;

use PhpCollective\Tracker\Trackable;
use Illuminate\Database\Eloquent\Model;

class Foo extends Model
{
    use Trackable;
    
    ...
}

Usage

Trackable trait provides three belongsTo relationship with Authenticated Users. creator(), editor(), destroyer()

$foo = App\Foo::first();
$foo->creator;
$foo->editor;
// If your model use soft delete
$foo->destroyer;

Credits

License

Laravel Model Tracker is open-sourced software licensed under the MIT license.

About

License:MIT License


Languages

Language:PHP 100.0%