Giter Site home page Giter Site logo

laravel-model-changes-history's Introduction

antonrom00/laravel-model-changes-history

Records the changes history made to an eloquent model.

Total Downloads Donate

Quick start

Your model must have an id field!

composer require antonrom00/laravel-model-changes-history
php artisan vendor:publish --tag="model-changes-history"
php artisan migrate

Note: this library use database storage as default.

Installation

composer require antonrom00/laravel-model-changes-history

The package is auto discovered.

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

php artisan vendor:publish --provider="Antonrom\ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="config"

You can use three ways for record changes: 'storage' => 'database', 'file' or 'redis'

If you want to use database storage, you must publish the migration file, run the following artisan commands:

php artisan vendor:publish --provider="Antonrom\ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="migrations"
php artisan migrate

Use this environment to manage library:

# Global recorgin model changes history
RECORD_CHANGES_HISTORY=true

# Default storage for recorgin model changes history
MODEL_CHANGES_HISTORY_STORAGE=database

Explore the config for more detailed library setup.

Usage

Add the trait to your model class you want to record changes history for:

use Antonrom\ModelChangesHistory\Traits\HasChangesHistory;
use Illuminate\Database\Eloquent\Model;

class TestModel extends Model {
    use HasChangesHistory;

    /**
     * The attributes that are mass assignable.
     * This will also be hidden for changes history.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}

Your model now has a relation to all the changes made:

$testModel->latestChange();

Antonrom\ModelChangesHistory\Models\Change {
    ...
    #attributes:  [
        "model_id" => 1
        "model_type" => "App\TestModel"
        "before_changes" => "{...}"
        "after_changes" => "{...}"
        "change_type" => "updated"
        "changes" => "{
            "title": {
                "before": "Some old title",  
                "after": "This is the new title"
            },
            "body": {
                "before": "Some old body",  
                "after": "This is the new body"
            },
            "password": {
                "before": "[hidden]",  
                "after": "[hidden]"
            }
        }"
        "changer_type" =>  "App\User"
        "changer_id" => 1
        "stack_trace" => "{...}"
        "created_at" => "2020-01-21 17:34:31"
    ]
    ...
}

Getting all changes history:

$testModel->historyChanges();

Illuminate\Database\Eloquent\Collection {
  #items: array:3 [
    0 => Antonrom\ModelChangesHistory\Models\Change {...}
    1 => Antonrom\ModelChangesHistory\Models\Change {...}
    2 => Antonrom\ModelChangesHistory\Models\Change {...}
    ...
}

If you use database storage you can also use morph relations to Change model:

$testModel->latestChangeMorph();
$testModel->historyChangesMorph();

Clearing changes history:

$testModel->clearHistoryChanges();

Get an independent changes history:

use Antonrom\ModelChangesHistory\Facades\HistoryStorage;
...

$latestChanges = HistoryStorage::getHistoryChanges(); // Return collection of all latest changes
$latestChanges = HistoryStorage::getHistoryChanges($testModel); // Return collection of all latest changes for model

$latestChange = HistoryStorage::getLatestChange(); // Return latest change
$latestChange = HistoryStorage::getLatestChange($testModel); // Return latest change for model

HistoryStorage::deleteHistoryChanges(); // This will delete all history changes
HistoryStorage::deleteHistoryChanges($testModel); // This will delete all history changes for model

Getting model changer:

// Return Authenticatable `changer_type` using HasOne relation to changer_type and changer_id
$changer = $latestChange->changer; 
If you use database storage you can use Change model as:
use Antonrom\ModelChangesHistory\Models\Change;

// Get the updates on the given model, by the given user, in the last 30 days:
Change::query()
    ->whereModel($testModel)
    ->whereChanger($user)
    ->whereType(Change::TYPE_UPDATED)
    ->whereCreatedBetween(now()->subDays(30), now())
    ->get();

Clearing changes history using console:

php artisan changes-history:clear

You can use it in Kelner:

protected function schedule(Schedule $schedule)
{
    $schedule->command('changes-history:clear')->monthly();
}

Donation

If this project help you reduce time to develop, you can buy me a cup of coffee buy_me_a_coffee =)

paypal

laravel-model-changes-history's People

Contributors

aburakovskiy avatar antonrom00 avatar katenkka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-model-changes-history's Issues

Use of different storages

If I want to use only one store, then why create them all?
This carries overhead in view of additional memory and query time.
code: src/Stores/HistoryStorageRegistry.php -> create().

Array to string conversion

the newly added code in ChangesHistoryService breaks in case of json fields which are casted to array.

if (empty($changes)) {
    $changes = array_diff_assoc($model->toArray(), $originalModel->toArray());
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.