minex/laravel-change-history

Package to record a changes performed on model

dev-master 2024-07-22 13:28 UTC

This package is auto-updated.

Last update: 2024-10-22 13:59:19 UTC


README

Package to record a changes performed on model.

Installation

composer require minex/laravel-change-history

Package provider is auto discovered.

To publish package migrations use:

php artisan vendor:publish --tag="lch-migrations"

After that run migrations:

php artisan migrate

To change the config, publish it file using the following command:

php artisan vendor:publish --tag="lch-config"

Usage

To record changes for a model add HasChangeHistory trait for it:

use Minex\LaravelChangeHistory\Traits\HasChangeHistory;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model {
    use HasChangeHistory;

    /**
     * Attributes in $hidden array will be replaced to **** in history record.
     *
     * @var array
     */
    protected $hidden = ['password', 'other_field'];
}

Note that history based on model events and will not be triggered using query operations

Example:

#This will NOT save a change record
SomeModel::where('is_active', true)->update([
    'is_active' => false,
]);

#This will save a change record
$model = SomeModel::find(1);
$model->is_active = true;
$model->save();

Configuration

If you want to ignore some attributes in history use:

use Minex\LaravelChangeHistory\Traits\HasChangeHistory;
use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model {
    use HasChangeHistory;

    public array $lch_ignore_attrs = ['updated_at', 'deleted_at'];
}