Adds Blameable support to Eloquent models in Laravel 5

4.0.0 2020-02-29 10:59 UTC

This package is auto-updated.

Last update: 2023-04-29 00:52:21 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:

  1. Run composer require nstapelbroek/culpa,
  2. Add to the providers list in config/app.php: "Culpa\CulpaServiceProvider",
  3. 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.