PHPackages                             ringlesoft/db-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. ringlesoft/db-archive

ActiveLaravel-package[Database &amp; ORM](/categories/database)

ringlesoft/db-archive
=====================

A Laravel package for archiving old database entries automatically

0.0.3(4w ago)4559↓55.6%1MITPHPPHP &gt;=8.1

Since Mar 9Pushed 4w ago2 watchersCompare

[ Source](https://github.com/ringlesoft/db-archive)[ Packagist](https://packagist.org/packages/ringlesoft/db-archive)[ Docs](https://ringlesoft.com/packages/db-archive)[ RSS](/packages/ringlesoft-db-archive/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (3)Dependencies (17)Versions (6)Used By (0)

Laravel DB Archive
==================

[](#laravel-db-archive)

Easily archive your Laravel database tables periodically to keep your application database lean and performant.

---

[![Latest Version on Packagist](https://camo.githubusercontent.com/76679bdaddf3274c89be89290540cd2ca30657b3932d8697d244d85529dce783/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72696e676c65736f66742f64622d617263686976652e737667)](https://packagist.org/packages/ringlesoft/db-archive)[![Total Downloads](https://camo.githubusercontent.com/648a1554f6e8ed88c784986d26961477ebcfc86173b4b17c0bc5450f31629c2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72696e676c65736f66742f64622d617263686976652e737667)](https://packagist.org/packages/ringlesoft/db-archive)[![PHP Version Require](https://camo.githubusercontent.com/85d02c2e2d902336a36b4bb3b411312cd0a1a5b25286e58cf13ed44ea862c430/68747470733a2f2f706f7365722e707567782e6f72672f72696e676c65736f66742f64622d617263686976652f726571756972652f706870)](https://packagist.org/ringlesoft/db-archive)[![Dependents](https://camo.githubusercontent.com/2dae2a3be8e066ab29b36a7087af26db8bda192bd4e2ad909e30aec809d01a9b/68747470733a2f2f706f7365722e707567782e6f72672f72696e676c65736f66742f64622d617263686976652f646570656e64656e7473)](https://packagist.org/packages/ringlesoft/db-archive)

---

Introduction
------------

[](#introduction)

`Laravel DB Archive` is a package that provides a simple and efficient way to archive old records from your database tables in Laravel applications. It helps maintain your application's database performance by moving historical data to archive tables, while keeping your primary tables focused on recent and relevant information.

> Laravel 10.x and above

Features
--------

[](#features)

- **Automated Data Archiving**: Move old data to archive tables based on configurable age thresholds
- **Database Connection Separation**: Keep your archive in a separate database connection
- **Batch Processing**: Process large tables in manageable batches to prevent memory issues
- **Flexible Configuration**: Configure different archive settings per table
- **Laravel Queue Integration**: Use Laravel's queue system for background processing
- **Conditional Archiving**: Archive only records that match specific conditions
- **Model Integration**: Use the `ArchivesData` trait to easily access archived records from your models
- **Command Line Interface**: Run archive operations and check status via Artisan commands
- **Comprehensive Logging**: Keep track of all archive operations with detailed logs

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

[](#installation)

### Step 1: Install The Package

[](#step-1-install-the-package)

You can install the package via composer:

```
composer require ringlesoft/db-archive
```

### Step 2: Publish Configuration

[](#step-2-publish-configuration)

Publish the configuration file with:

```
php artisan vendor:publish --provider="RingleSoft\DbArchive\DbArchiveServiceProvider" --tag="config"
```

### Step 3: Setup Archive Database Connection

[](#step-3-setup-archive-database-connection)

Define your archive database connection in your `config/database.php` file. You can easily do this by clonning your default database and changing its properties. For example:

```
'databases' => [
    ...,
    'mysql_archive' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => 'archive_database',
        'username' => 'root',
        'password' => 'password',
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
    ],
],
```

After this, use the `ARCHIVE_DB_CONNECTION` environment variable to specify the connection name for archive operations.

```
ARCHIVE_DB_CONNECTION=mysql_archive
```

### Step 4: Configure Your Tables

[](#step-4-configure-your-tables)

In your `config/db_archive.php` file, define the tables you want to archive and their associated settings. For example:

```
'tables' => [
    'orders',
    'activity_logs',
    'audit_trail'
],
```

### Step 5: Run Setup Command

[](#step-5-run-setup-command)

Run the `setup` [command](#setting-up) to create the archive database and tables:

```
php artisan db-archive:setup
```

### Step 6: Start Archiving

[](#step-6-start-archiving)

You can now start archiving your data using the `archive` [command](#artisan-command) or by [scheduling](#scheduling) it using a cron job. You can also implement the `ArchivesData` trait in your models to access their archived records.

---

Configuration
-------------

[](#configuration)

### Available Configuration Options:

[](#available-configuration-options)

#### `connection`:

[](#connection)

- The database connection name to be used for creating archive tables and moving data.
- Ensure this connection is defined in your `config/database.php` file.
- I recommend using a different connection from your application's default connection.
- Defaults to `mysql_archive` and can be overridden using the `ARCHIVE_DB_CONNECTION` environment variable.

#### `settings`:

[](#settings)

- `table_prefix`:

    - Prefix to be added to the archived tables (e.g., archive\_).
    - Set to `null` for no prefix.
- `batch_size`:

    - Number of records to process in each batch during archiving.
    - Adjust this value based on your server resources and table size.
    - Defaults to 1000.
- `date_column`:

    - The database column used to determine the age of records for archiving (e.g., `created_at`, `updated_at`).
    - Defaults to `created_at`.
- `archive_older_than_days`:

    - Number of days after which records are considered old enough to be archived.
    - Records with a date in the `date_column` older than this value will be archived.
    - Defaults to `365` days .
- `conditions`:

    - An array of additional where conditions to filter records for archiving.
    - Allows for more specific criteria for selecting records to archive.
    - Defaults to an empty array `[]`.
    - Example: `[['status', 'active']]` or `[['id', '
