nowendwell/laravel-archivable

A trait to make an Eloquent model archivable

v1.0.1 2019-04-05 16:03 UTC

This package is auto-updated.

Last update: 2024-11-06 04:54:34 UTC


README

Latest Version on Packagist Total Downloads

This package contains a trait to make Eloquent models archivable. This works in a similar way to the SoftDelete trait that Laravel ships with.

Once you have applied to the trait to a model you can do the following:

// to archive a User
$user = User::find(1);
$user->archive();

// to unarchive a User
$user = User::withArchived()->find(1);
$user->unarchive();

Installation

You can install the package via composer:

composer require nowendwell/laravel-archivable

Making a model archivable

To make model archivable add the Nowendwell\LaravelArchivable\Archivable trait to the model you wish to archive

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

class User extends Model
{
    use Archivable;
}

Usage

This traits adds a Global Query Scope to exclude any models that have a value in the archived_at column.

Archiving

// to archive a User
$user = User::find(1);
$user->archive();

Unarchiving

// to unarchive a User
$user = User::withArchived()->find(1);
$user->unarchive();

Checking Archive Status

// to unarchive a User
$user = User::withArchived()->find(1);
var_dump( $user->archived() ); // bool true/false

Query Scopes

User::withArchived()->get(); // returns all users
User::withOutArchived()->get(); // returns users that are not archived, same results as User::all()
User::onlyArchived()->get() // returns only users that have a value in the archived_at column

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 nowendwell@gmail.com 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.