24
MayLaravel Archivable is 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.
You can install the package via composer:
composer require joelbutcher/laravel-archivable
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.
For more details and source code, Please visit Github
Published at : 24-05-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project