Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Archivable Laravel Package

joelbutcher/laravel-archivable

Add archiving to Laravel Eloquent models like SoftDeletes. Includes migration macros for archived_at, model trait, and query builder scopes/macros to archive/unarchive records and filter archived vs active results in fluent chains.

View on GitHub
Deep Wiki
Context7

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
});
Rollback
Schema::create('posts', function (Blueprint $table) {
    $table->dropArchivedAt();
});

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, isArchived, withArchived, withoutArchived, onlyArchived and can be used accordingly:

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

// Check Archive status
$user->isArchived();

$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.

Archived models in route implicit binding

Typically, implicit model binding will not retrieve models that have been archived. However, you may instruct the implicit binding to retrieve these models by chaining the withArchived method onto your route's definition:

use App\Models\User;
 
Route::get('/users/{user}', function (User $user) {
    return $user->email;
})->withArchived();

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4