constant-null/eloquent-changed-by-user

This simple trait written for eloquent allows you to automaticly write down id of last user changed the database record.

v0.1.1 2015-12-09 04:30 UTC

This package is not auto-updated.

Last update: 2024-12-19 01:16:45 UTC


README

Please be aware that this trait uses Auth facade and expects it to implement Illuminate\Contracts\Auth\Guard contract

How it works

Quite simple actually, just before saving row to database, this trait gets id of current user using Auth::user()->id (thats why requirements above exists) and write it to specified database field (for more specifics read the Installation and configuration section)

Installation and configuration

This trait can be installed via composer. Just add following to your composer.json file:

{
    "require": {
        "constant-null/eloquent-changed-by": "~0.1"
    }
}

and then run:

$ composer update

To start using this trait you need to import it to Eloquent model

<?php

use ConstantNull\Eloquent\Support\ChangedByUser;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model
{
    use ChangedByUser;

    /* The rest of the your class */
}

and add column to database in which user id will be stored. Like this for example:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class BestMigrationEver extends Migration
{

    public function up()
    {
        Schema::table('some_table', function(Blueprint $table) {
            $table->integer('changed_by')->nullable();
        });
    }

How you can see in example the default column name for user id is changed_by, but it can be easily changed by specifying constant CHANGED_BY in your class body.

For example i want this column name to be 'last_user_id':

<?php

use ConstantNull\Eloquent\Support\ChangedByUser;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model
{
    use ChangedByUser;

    const CHANGED_BY = 'last_user_id';

    /* Rest part of the your class */
}

Thats all!