digitalcloud/laravel-blameable

Laravel package allow you to add created/updated by attributes on eloquent models

v1.1 2019-01-14 13:02 UTC

This package is auto-updated.

Last update: 2024-11-18 22:29:56 UTC


README

Latest Stable Version Total Downloads

Laravel Blameable.

This package allow you to track the creator, updater and deleter of eloquent models.

Installation

You can install the package via composer:

composer require digitalcloud/laravel-blameable

In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

    'providers' => [
        DigitalCloud\Blameable\BlameableServiceProvider::class,
    ];

You can publish the config file with:

    php artisan vendor:publish --provider="DigitalCloud\Blameable\BlameableServiceProvider" --tag="config"

When published, the config/blameable.php config file contains:

<?php

return [
    'column_names' => [
        'createdByAttribute' => 'created_by',
        'updatedByAttribute' => 'updated_by',
        'deletedByAttribute' => 'deleted_by',
    ],
     'models' => [
         'user' => \App\User::class
     ]
];

You can update the columns names in this file, or you can stack with default names. If you are not using the default laravel App\User model you need to provide the model class.

Usage Example

First, you need to add the DigitalCloud\Blameable\Traits\Blameable trait to your model(s). For example:

<?php

namespace App;

use DigitalCloud\Blameable\Traits\Blameable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Blameable;
}

Then, you need to make sure that the database table for the model has the required columns. Luckily we provide two ways to do this task:

  • By using console command and provide the model which you need to add columns:

      php artisan blameable:add-blameable-columns App\Post
  • By calling addBlameableColumns() on the model uses DigitalCloud\Blameable\Traits\Blameable trait:

      \App\Post::addBlameableColumns();

By using DigitalCloud\Blameable\Traits\Blameable in your model, the package will fill those columns automatically after creating, updating and deleting the model.

Relations

To get the creator/editor instance you can use:

$post = \App\Post::find(1);
$creator = $post->creator;
$editor = $post->editor;
$deletor = $post->deletor;

Note:

The package allow you to add blame columns to your migrations, using blameable() functions, for example:

    Schema::table($table, function (Blueprint $table) {
        // this will add created_by, updated_by and updated_by columns on your table.
        $table->blameable();
    });