Arkitecht / laravel-attributions by Arkitecht

Laravel 5 - Track attributions (model creator / updater)
9,996
4
3
Package Data
Maintainer Username: Arkitecht
Maintainer Contact: aaron@arkitech.net (Aaron Rubin)
Package Create Date: 2015-08-31
Package Last Update: 2023-06-01
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-19 15:09:19
Package Statistics
Total Downloads: 9,996
Monthly Downloads: 45
Daily Downloads: 1
Total Stars: 4
Total Watchers: 3
Total Forks: 1
Total Open Issues: 0

Laravel Attributions

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;
}
?>