├── README.md ├── composer.json ├── config └── blameable.php └── src ├── BlameableServiceProvider.php ├── Commands └── AddBlameableColumns.php ├── Traits └── Blameable.php └── helpers.php /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/digitalcloud/laravel-blameable/v/stable)](https://packagist.org/packages/digitalcloud/laravel-blameable) 2 | [![Total Downloads](https://poser.pugx.org/digitalcloud/laravel-blameable/downloads)](https://packagist.org/packages/digitalcloud/laravel-blameable) 3 | 4 | # Laravel Blameable. 5 | 6 | This package allow you to track the creator, updater and deleter of eloquent models. 7 | 8 | ## Installation 9 | 10 | You can install the package via composer: 11 | 12 | ```bash 13 | 14 | composer require digitalcloud/laravel-blameable 15 | 16 | ``` 17 | 18 | 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: 19 | 20 | ```php 21 | 22 | 'providers' => [ 23 | DigitalCloud\Blameable\BlameableServiceProvider::class, 24 | ]; 25 | 26 | ``` 27 | 28 | 29 | You can publish the config file with: 30 | 31 | ```bash 32 | 33 | php artisan vendor:publish --provider="DigitalCloud\Blameable\BlameableServiceProvider" --tag="config" 34 | 35 | ``` 36 | 37 | When published, the config/blameable.php config file contains: 38 | 39 | ```php 40 | [ 44 | 'createdByAttribute' => 'created_by', 45 | 'updatedByAttribute' => 'updated_by', 46 | 'deletedByAttribute' => 'deleted_by', 47 | ], 48 | 'models' => [ 49 | 'user' => \App\User::class 50 | ] 51 | ]; 52 | 53 | ``` 54 | 55 | You can update the columns names in this file, or you can stack with default names. 56 | If you are not using the default laravel `App\User` model you need to provide the model class. 57 | 58 | 59 | ## Usage Example 60 | 61 | First, you need to add the `DigitalCloud\Blameable\Traits\Blameable` trait to your model(s). For example: 62 | 63 | ```php 64 | creator; 101 | $editor = $post->editor; 102 | $deletor = $post->deletor; 103 | ``` 104 | 105 | ### Note: 106 | 107 | The package allow you to add blame columns to your migrations, using `blameable()` functions, for example: 108 | 109 | ```php 110 | 111 | Schema::table($table, function (Blueprint $table) { 112 | // this will add created_by, updated_by and updated_by columns on your table. 113 | $table->blameable(); 114 | }); 115 | 116 | ``` 117 | 118 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "digitalcloud/laravel-blameable", 3 | "description": "Laravel package allow you to add created/updated by attributes on eloquent models", 4 | "keywords": [ 5 | "laravel", 6 | "package", 7 | "creator", 8 | "blameable", 9 | "eloquent" 10 | ], 11 | "license": "MIT", 12 | "require": { 13 | "php": ">=7.1.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "DigitalCloud\\Blameable\\": "src/" 18 | } 19 | }, 20 | "extra": { 21 | "laravel": { 22 | "providers": [ 23 | "DigitalCloud\\Blameable\\BlameableServiceProvider" 24 | ] 25 | } 26 | }, 27 | "config": { 28 | "sort-packages": true 29 | }, 30 | "minimum-stability": "dev", 31 | "prefer-stable": true 32 | } 33 | -------------------------------------------------------------------------------- /config/blameable.php: -------------------------------------------------------------------------------- 1 | [ 5 | 'createdByAttribute' => 'created_by', 6 | 'updatedByAttribute' => 'updated_by', 7 | 'deletedByAttribute' => 'deleted_by', 8 | ], 9 | 'models' => [ 10 | 'user' => \App\User::class 11 | ] 12 | ]; 13 | -------------------------------------------------------------------------------- /src/BlameableServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 15 | __DIR__.'/../config/blameable.php' => config_path('blameable.php'), 16 | ], 'config'); 17 | 18 | $this->registerMacroHelpers(); 19 | 20 | if ($this->app->runningInConsole()) { 21 | $this->commands([AddBlameableColumns::class]); 22 | } 23 | 24 | } 25 | 26 | public function register() { 27 | $this->mergeConfigFrom( 28 | __DIR__ . '/../config/blameable.php', 29 | 'blameable' 30 | ); 31 | } 32 | 33 | public function registerMacroHelpers() { 34 | Blueprint::macro('blameable', function () { 35 | $this->unsignedBigInteger(Config::get('blameable.column_names.createdByAttribute', 'created_by'))->nullable(); 36 | $this->unsignedBigInteger(Config::get('blameable.column_names.updatedByAttribute', 'updated_by'))->nullable(); 37 | $this->unsignedBigInteger(Config::get('blameable.column_names.deletedByAttribute', 'deleted_by'))->nullable(); 38 | }); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/Commands/AddBlameableColumns.php: -------------------------------------------------------------------------------- 1 | argument('model'); 19 | $table = (new $model)->getTable(); 20 | $createdByAttribute = Config::get('blameable.column_names.createdByAttribute', 'created_by'); 21 | $updatedByAttribute = Config::get('blameable.column_names.updatedByAttribute', 'updated_by'); 22 | $deletedByAttribute = Config::get('blameable.column_names.deletedByAttribute', 'deleted_by'); 23 | if (!Schema::hasColumn($table, $createdByAttribute) 24 | && !Schema::hasColumn($table, $updatedByAttribute) 25 | && !Schema::hasColumn($table, $deletedByAttribute)) { 26 | Schema::table($table, function (Blueprint $table) { 27 | $table->blameable(); 28 | }); 29 | } 30 | $this->info("Blameable columns for `{$this->argument('model')}` created"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Traits/Blameable.php: -------------------------------------------------------------------------------- 1 | $createdByAttribute = Auth::id(); 21 | }); 22 | 23 | static::updating(function ($model) { 24 | $updatedByAttribute = Config::get('blameable.column_names.updatedByAttribute', 'updated_by'); 25 | $model->$updatedByAttribute = Auth::id(); 26 | }); 27 | 28 | if (static::usesSoftDelete()) { 29 | static::deleting(function ($model) { 30 | $deletedByAttribute = Config::get('blameable.column_names.deletedByAttribute', 'deleted_by'); 31 | $model->$deletedByAttribute = Auth::id(); 32 | $model->save(); 33 | }); 34 | } 35 | } 36 | 37 | public static function checkBlameableColumns() { 38 | $table = (new static)->getTable(); 39 | $createdByAttribute = Config::get('blameable.column_names.createdByAttribute', 'created_by'); 40 | $updatedByAttribute = Config::get('blameable.column_names.updatedByAttribute', 'updated_by'); 41 | $deletedByAttribute = Config::get('blameable.column_names.deletedByAttribute', 'deleted_by'); 42 | if (!Schema::hasColumn($table, $createdByAttribute) 43 | && !Schema::hasColumn($table, $updatedByAttribute) 44 | && !Schema::hasColumn($table, $deletedByAttribute)) { 45 | // 46 | } 47 | } 48 | 49 | public static function addBlameableColumns() { 50 | $table = (new static)->getTable(); 51 | $createdByAttribute = Config::get('blameable.column_names.createdByAttribute', 'created_by'); 52 | $updatedByAttribute = Config::get('blameable.column_names.updatedByAttribute', 'updated_by'); 53 | $deletedByAttribute = Config::get('blameable.column_names.deletedByAttribute', 'deleted_by'); 54 | if (!Schema::hasColumn($table, $createdByAttribute) 55 | && !Schema::hasColumn($table, $updatedByAttribute) 56 | && !Schema::hasColumn($table, $deletedByAttribute)) { 57 | Schema::table($table, function (Blueprint $table) { 58 | $table->blameable(); 59 | }); 60 | } 61 | } 62 | 63 | public function creator() { 64 | $userModel = Config::get('blameable.models.user', User::class); 65 | return $this->belongsTo($userModel, 'created_by', 'id'); 66 | } 67 | 68 | public function editor() { 69 | $userModel = Config::get('blameable.models.user', User::class); 70 | return $this->belongsTo($userModel, 'updated_by', 'id'); 71 | } 72 | 73 | public function deletor() { 74 | $userModel = Config::get('blameable.models.user', User::class); 75 | return $this->belongsTo($userModel, 'deleted_by', 'id'); 76 | } 77 | 78 | protected static function usesSoftDelete() 79 | { 80 | static $softDelete; 81 | 82 | if (is_null($softDelete)) { 83 | $instance = new static; 84 | return $softDelete = method_exists($instance, 'bootSoftDeletes'); 85 | } 86 | 87 | return $softDelete; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 |