nstapelbroek / culpa
Adds Blameable support to Eloquent models in Laravel 5
Installs: 73 315
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 4
Forks: 5
Open Issues: 2
Requires
- illuminate/config: ^6.0
- illuminate/events: ^6.0
- illuminate/support: ^6.0
Requires (Dev)
- ext-sqlite3: *
- illuminate/database: ^6.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^5.0
Conflicts
This package is auto-updated.
Last update: 2024-06-03 07:29:08 UTC
README
Blameable extension for Laravel 5 & 6 Eloquent ORM models. This extension automatically adds references to the authenticated user when creating, updating or soft-deleting a model.
Disclaimer: I've created this repository because I didn't want to include a dev-master version of the original fork in my composer.json. Once the original pull request gets merged, this package will probably become deprecated and you'll be best of switching back within a year. Due to changes in the namespace and directory structure this package is not backwards compatible with the original Culpa for laravel < 4.
Installation
To install the package in your project:
- Run
composer require nstapelbroek/culpa
, - Add to the
providers
list in config/app.php:"Culpa\CulpaServiceProvider"
, - Publish the configuration to your application:
php artisan vendor:publish
Usage
You can add auditable fields on a per-model basis by adding a protected property
and a model observer. The property $blameable
contains events you wish to
record - at present this is restricted to created, updated and deleted - which
function the same as Laravel's timestamps.
use Culpa\Traits\Blameable; use Culpa\Traits\CreatedBy; use Culpa\Traits\DeletedBy; use Culpa\Traits\UpdatedBy; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use Blameable, CreatedBy, UpdatedBy, DeletedBy; protected $blameable = array('created', 'updated', 'deleted'); // Other model logic here }
- On create, the authenticated user will be set in
created_by
, - On create and update, the authenticated user will be set in
updated_by
, - If the model was soft-deletable, the authenticated user will be
set in
deleted_by
on deletion.
To activate the automatic updating of these fields, you need to add the blamable trait to the model. That's it! Need more tweak options for Culpa? take a look at the [Tweaks and Configuration docs](docs/1. Tweaks and Configuration.md).
License
Culpa is released under the MIT License.