pderas/archivalist

9,580
Install
composer require pderas/archivalist
Latest Version:v2.2.0
PHP:^7.3|^8.0
License:MIT
Last Updated:Mar 9, 2023
Links: GitHub  ·  Packagist
Maintainer: reed-jones

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 reed.jones@pderas.com instead of using the issue tracker.

Credits

License

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

Related Packages

gabrieloliverio/laravel5-generators

Database metadata-based generators for Laravel 5

1
erirk/paypalpayment

laravel-paypalpayment is simple package help you process direct credit card paym...

0
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925
laravel/tinker

Powerful REPL for the Laravel framework.

489,754,436 7,444