A simple package for making Laravel Eloquent models 'cancellable'. This package allows for the easy cancelling of models by creating various macros to be used within method chaining.
This package requires PHP 7.3 or higher and Laravel 6.0 or higher.
You can install the package via composer:
composer require jeanlrnt/laravel-cancellable
The Cancellable
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 cancelledAt
macro to your migration, like so:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->timestamps();
$table->cancelledAt(); // Macro
});
You can now, safely, include the Cancellable
trait in your Eloquent model:
namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
use \LaravelCancellable\Cancellable;
class Post extends Model {
use Cancellable;
...
}
The extensions shipped with this trait include; cancel
, unCancel
, withCancelled
, withoutCancelled
, onlyCancelled
and can be used accordingly:
$user = User::first();
$user->cancel();
$user->unCancel();
$usersWithCanceled = User::query()->withCanceled();
$onlyCanceledUsers = User::query()->onlyCanceled();
By default, the global scope of this trait uses the withoutCanceled
extension when the trait is added to a model.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] 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.