pderas / archivalist
Installs: 4 637
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 3
Requires
- php: ^7.3|^8.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
- staudenmeir/eloquent-json-relations: ^1.1
Requires (Dev)
- orchestra/testbench: ^5.0
- pestphp/pest: ^0.2.3
- phpunit/phpunit: ^9.0
README
Archivalist is a minimal package designed to make archiving changes to Laravel models easy.
Installation
You can install the package via composer:
composer require pderas/archivalist
Usage
Simply add the Pderas\Archivalist\Traits\HasArchives
to any model you wish to archive.
use Pderas\Archivalist\Traits\HasArchives; class Post extends Model { use HasArchives; }
If you wish certain columns to always be archived, this can be accomplished by adding wither a archived
property or method to the model
use Pderas\Archivalist\Traits\HasArchives; class Post extends Model { use HasArchives; protected $archived = [ 'updated_at' ]; // Alternatively... protected function archived() { return [ 'updated_at' => $this->getOriginal('updated_at'), 'is_archived' => true ]; } }
Archives can be 'rehydrated' into the state of the original model
$user->company = 'Pderas'; $user->save(); $archive = $user->archives()->first(); // => \Pderas\Archivalist\Models\Archive $original = $archive->rehydrate(); // => \App\User
A Collection with the full history of the model can be acquired using ->getHistory()
$user->getHistory(); // A user model for every state the user was in
Mass assignment is not supported, in which case you must use the following workaround:
// Do not do Post::where('status','open') ->update(['status','closed']); // This will fail // Do this instead Archivalist::proxy(Post::query()) ->where('status','open') ->update(['status','closed']);
Support for beforeArchive has been added. Implement the method beforeArchive
on any model which uses HasArchives
trait to run any extra logic before the primary archives logic is ran.
// SomeModel which uses HasArchives public function beforeArchive() { // Put logic here to be ran before the primary archives logic is ran // useful for automating tasks such as always writing user->id, etc }
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email reed.jones@pderas.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.