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();
You can install the package via composer:
composer require nowendwell/laravel-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;
}
This traits adds a Global Query Scope to exclude any models that have a value in the archived_at
column.
// to archive a User
$user = User::find(1);
$user->archive();
// to unarchive a User
$user = User::withArchived()->find(1);
$user->unarchive();
// to unarchive a User
$user = User::withArchived()->find(1);
var_dump( $user->archived() ); // bool true/false
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
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email nowendwell@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.