Skip to content

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

Notifications You must be signed in to change notification settings

Arkitecht/laravel-attributions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages