/laravel-model-trackable

🔍 Laravel package that allows you to track and log nested changes applied on your models

Primary LanguagePHPMIT LicenseMIT

logo


Latest Version on Packagist

A laravel package that allows you to track and log nested changes applied on your (models, and their relations) using a single Trait

Installation

You can install the package via composer:

composer require mouadziani/laravel-model-trackable

Simple Usage

  • Firstly you have to apply trackable trait on your model
use LaravelModelTrackable\Traits\Trackable;

class ModelName extends Model
{
    use Trackable;

    //
}
  • In case you want to track the changes applied on your model's relationships, you need to add an attribute in your model called $toBeLoggedRelations which must contain an array of relationships like the example below
use LaravelModelTrackable\Traits\Trackable;

class ModelName extends Model
{
    use Trackable;

    public $toBeLoggedRelations = ['relation1', 'relation2'];
}
  • Then, you can get an array that should contains all changes applied on your model after every update
$model = ModelName::update([
    ...
]);

// Get list of changed attributes
$model->getChangedAttributes();

Disclaimer

Currently this package can't track hasMany, ManyToMany or MorphMany relations

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email mouad.ziani1997@gmail.com instead of using the issue tracker.

Credits

Licence

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

featured_repository