Sometimes you only need a simple way to manage statuses.
You can install the package via composer:
composer require alejandrotrevi/laravel-ankal
Optionally, you can publish the migration included with the library. The included migration is a good place to add the necessary columns to your tables or perhaps remove some columns from existing tables.
php artisan vendor:publish --provider="Alejandrotrevi\LaravelAnkal\LaravelAnkalServiceProvider" --tag="migrations"
Add the HasStatuses
trait to a model.
use Alejandrotrevi\LaravelAnkal\HasStatuses;
class MyModel extends Model
{
use HasStatuses;
}
Add the necessary migrations to each of the tables on which you will use the statuses.
Schema::create('my_table', function (Blueprint $table) {
$table->statusColumns();
});
behind the scenes this basically adds 3 columns: status
, reason
and status_updated_at
.
Optionally you can set a default status for that table, you simply pass an additional argument to the statusColumns()
table modifier this additional argument is the default status the status
column will have when you create a new model.
Schema::create('my_table', function (Blueprint $table) {
$table->statusColumns('my_default_status');
});
You can set a new status like this:
$model->setStatus('status');
You can also provide a reason for the status modification.
$model->setStatus('status', 'why this status changed?');
Since the status exist on the same table you simply call the status as another property on your model.
$model->status;
$model->reason;
$model->status_updated_at;
You have 2 scopes available for your models currentStatus
and exceptStatus
.
// All models with status "status"
Model::currentStatus('status');
// All models with status "status" or "other-status"
Model::currentStatus('status', 'other-status');
Model::currentStatus(['status', 'other-status']);
Without a given status:
// All models except those with the "my-status" status
Model::exceptStatus('my-status');
// All models except those with the "my-status" or "other-status" statuses.
Model::exceptStatus('my-status', 'other-status');
Model::exceptStatus(['my-status', 'other-status']);
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 alex_tf_1992@live.com.mx instead of using the issue tracker.
This package is heavily inspired in the spatie/laravel-model-status package, this aims to be a simpler version of Spatie's solution, every credit should go to them 🤗
The MIT License (MIT). Please see License File for more information.
Ankal means "To be" in the Mayan language.