Archive

The archiving package is designed for helping long term applications with data handling. You have the power to archive old data sets to JSON storage files in S3 based on time frames. Exports are encrypted by default using Laravel's APP_KEY.

Installation

composer require "grafite/archive"

Then publish the package assets:

php artisan vendor:publish --provider="Grafite\Archive\GrafiteArchiveProvider"

Configuration

The published config/archive.php file gives you control over the disk, path, default models, encryption, and query defaults:

[
    'disk' => env('ARCHIVE_DISK', env('FILESYSTEM_DISK', 'local')),
    'path' => env('ARCHIVE_PATH', 'archives'),

    // Models to archive when no --model option is passed
    'models' => [],

    'encryption' => [
        'enabled' => true,
        'driver' => 'crypt',
    ],

    'query' => [
        'chunk_size' => 500,
        'date_column' => 'created_at',
        'scopes' => [],
        'relationships' => [],
    ],

    'fixtures' => [
        'path' => 'fixtures',
        'seeders' => false,
    ],
]

Commands

The package provides an Artisan command to run the archive workflow against any Eloquent model.

archive:run

Archives model records into encrypted export files on the configured disk.

php artisan archive:run --model="App\\Models\\Post" --before="2024-12-31 23:59:59"

Options

Option Description
--model= Fully qualified model class to archive. Falls back to archive.models config if omitted.
--before= Archive rows with the date column value before this timestamp.
--after= Archive rows with the date column value after this timestamp.
--scope=* Apply one or more local scopes, optionally with colon-delimited arguments.
--without-scopes Ignore model or config default scopes.
--with=* Eager load one or more relationships into the archive payload.
--without-relationships Ignore model or config default relationships.
--disk= Override the filesystem disk defined in config.
--path= Override the target archive path defined in config.
--unencrypted Store the payload without encryption.
--dry-run Resolve records and display the result without writing a file.

Model Concern

Add the Archiveable trait to any model to customise its archive behaviour via properties.

use Grafite\Archive\Concerns\Archiveable;

class Post extends Model
{
    use Archiveable;

    protected string $archiveDateColumn = 'published_at';
    protected array $archiveScopes = ['published'];
    protected array $archiveRelationships = ['author', 'tags'];
    protected bool $shouldArchive = true;
}

The trait provides the following methods, each of which falls back to the corresponding config value when the property is not set on the model:

archiveDateColumn()      // column used to filter records by date (default: created_at)
archiveScopes()          // local scopes applied during the query
archiveRelationships()   // relationships eager loaded into the archive payload
archiveFixtureName()     // snake_case basename used when writing fixture files
shouldArchive()          // whether this model participates in archiving