joelbutcher / laravel-archivable by joelbutcher

An archivable trait package for Laravel Eloquent models
256,707
109
4
Package Data
Maintainer Username: joelbutcher
Maintainer Contact: joel@joelbutcher.co.uk (Joel Butcher)
Package Create Date: 2020-10-07
Package Last Update: 2024-03-15
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:13:40
Package Statistics
Total Downloads: 256,707
Monthly Downloads: 8,506
Daily Downloads: 455
Total Stars: 109
Total Watchers: 4
Total Forks: 19
Total Open Issues: 0

A simple package for making Laravel Eloquent models 'archivable'. This package allows for the easy archiving of models by creating various macros to be used within method chaining.

Installation

You can install the package via composer:

composer require joelbutcher/laravel-archivable

Usage

Migrations

The Archivable trait works similarly to Laravel's SoftDeletes trait. This package also ships with a helpful macro for Laravel's \Illuminate\Database\Schema\Blueprint. To get started, simply add the archivedAt macro to your migration, like so:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->string('title');
    $table->timestamps();
    $table->archivedAt(); // Macro
});

Eloquent

You can now, safely, include the Archivable trait in your Eloquent model:

namespace App\Models;

use \Illuminate\Database\Eloquent\Model;
use \LaravelArchivable\Archivable;

class Post extends Model {

    use Archivable;
    ...
}

Extensions

The extensions shipped with this trait include; archive, unArchive, withArchived, withoutArchived, onlyArchived and can be used accordingly:

$user = User::first();
$user->archive();
$user->unArchive();

$usersWithArchived = User::query()->withArchived();
$onlyArchivedUsers = User::query()->onlyArchived();

By default, the global scope of this trait uses the withoutArchived extension when the trait is added to a model.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email joel@joelbutcher.co.uk instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.