arkitecht/laravel-attributions

Laravel 5/6/7/8/9/10 - Track attributions (model creator / updater)

5.2 2024-07-08 16:18 UTC

This package is auto-updated.

Last update: 2024-12-08 17:14:35 UTC


README

Laravel 5.1 Source License

Easily attribute the creator / last updater of a model in your database.

Designed to work like (and alongside) $table->timestamps() in your Laravel migrations, Attributions introduces $table->attributions(). This will add creator_id and updater_id columns to your table to track the user that created and updated the model respectively. By default this uses the Laravel 5.1 predefined users table, but can be customized to reference any table and key combination.

Quick Installation

You can install the package most easily through composer

Laravel 5.1.x

composer require arkitecht/laravel-attributions

Schema Blueprint and Facades

Once this operation is complete, you can update the Schema Facade to point to our drop-in replacement, which uses our Blueprint extension class to add the attributions.

Laravel 5.1.x

Facade (in config/app.php)

'Schema' => Illuminate\Support\Facades\Schema::class,

'Schema' => Arkitecht\Attributions\Facades\Schema::class,

You can also manually use the attributions builder, without overwriting the Facade like so:

use Arkitecht\Attributions\Database\Schema\Blueprint;

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    $schema =  DB::getSchemaBuilder();

    $schema->blueprintResolver(function($table, $callback) {
        return new Blueprint($table, $callback);
    });

    $schema->create('tests', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->attributions();
    });
}

Using it in your migrations

To have your migration add the attribution columns, just call the Blueprint::attributions method.

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->attributions();
        });
    }
	
	...

}

You can also have it reference an alternate table (from users) or key (from id).

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->attributions('employees','employee_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tests');
    }
}

Using it in your model

To have the creator and updater automagically updated when a model is created and updated, just use the Attributions trait in your model.

<?php

namespace App;

use Arkitecht\Attributions\Traits\Attributions;
use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    use Attributions;
}
?>