PHPackages                             lab2view/laravel-model-archive - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. lab2view/laravel-model-archive

ActiveLibrary[Database &amp; ORM](/categories/database)

lab2view/laravel-model-archive
==============================

...

v1.0(3mo ago)210.3k↓76.8%MITPHPPHP ^8.1|^8.2|^8.3

Since Apr 1Pushed 2w agoCompare

[ Source](https://github.com/lab2view/laravel-model-archive)[ Packagist](https://packagist.org/packages/lab2view/laravel-model-archive)[ RSS](/packages/lab2view-laravel-model-archive/feed)WikiDiscussions main Synced 3d ago

READMEChangelogDependencies (6)Versions (5)Used By (0)

lab2view/laravel-model-archive
==============================

[](#lab2viewlaravel-model-archive)

A simple package to make Laravel Eloquent models "archivable". This package allows to easily move rarely accessed data (due to its duration for example) to a secondary database and provides the necessary methods to retrieve it when needed; thus reading relevant (frequently accessed) data from the primary database will be faster.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require lab2view/laravel-model-archive
```

Usage
-----

[](#usage)

#### Migrations

[](#migrations)

Archiving is done in 2 steps. The first is to copy the models marked `archivable` into the database dedicated to archives and the second step is to verify that the archiving of said data (models) has been done and to delete them from the main database. This package to work must create a polymorphic table intended to save the archived archivable models.

```
Schema::create('archives', function (Blueprint $table) {
    $table->id();
    $table->string("archive_with");
    $table->morphs("archivable");
    $table->timestamp('validated_at')->nullable();
    $table->timestamps();
});
```

To publish the migration file use

```
php artisan vendor:publish --provider="Lab2view\ModelArchive\ModelArchiveServiceProvider" --tag="migrations"

```

#### Commands

[](#commands)

This package provides 2 commands, dedicated respectively to copy the archivable models to the database dedicated to archiving and a second one to validate the archived data and delete this well-archived data from the main database.

```
lab2view:model-archive
lab2view:validate-model-archive

```

These commands require direct manipulation with the database by running queries on the model defined as archivable. For example, using the `LadaCacheTrait` trait from the spiritix/lada-cache package means that queries on models do not always return the `Illuminate\Database\Eloquent\Builder` interface, but a cache-based builder version specific to this package. In this case, it is necessary to run the `lada-cache:desable` command before the commands and `lada-cache:enable` after.

#### Between commands

[](#between-commands)

It is possible to register callables that will be called before or after commands or a specific command by registering them in the `between_commands` configuration. Example:

```
