GitHub - laravel-enso/HistoryTracker: Laravel Model history tracking made easy
source link: https://github.com/laravel-enso/HistoryTracker
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
HistoryTracker
Simple to use, customizable Laravel Model history tracking utility trait
Laravel Enso already includes this package
Details
The trait helps keep track of the changes made to a model by saving a snapshot for each relevant update of the model to a different 'history' table.
The records in the history table are linked via FK to the original model. Setting a FK constraint on the DB column may be set if necessary.
Usage
You can choose the attributes you want to track from the model by declaring them as fillable in the history model.
When the model is created an initial snapshot of the desired attributes is saved. When the model is updated, if any one of the tracked attributes has changed, a new snapshot/history entry is persisted.
-
Create a histories table, such as
model_histories
- where model is what you need to keep track of. -
Create a corresponding history model, such as
ModelHistory
and add as fillable the attributes you want tracked:protected $fillable = ['tracked', 'attributes', 'list'];
Don't forget to also have the FK column for the relationship to the tracked model.
-
Use the trait in the tracked model:
use HistoryTracker;
-
Add the
$historyModel
property to the tracked model:protected $historyModel = ModelHistory::class;
The trait also defines a relationship to the history model instances, so you can access the snapshots by calling histories
:
$myModel->histories
Notes
Laravel Enso already included and ready to use out of the box. The package itself has no other dependencies beyond Laravel.
Contributions
are welcome. Pull requests are great, but issues are good too.
License
This package is released under the MIT license.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK