Skip to content

PDERAS/archivalist

Repository files navigation

Archivalist

Latest Version on Packagist Total Downloads

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 [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.