fabpl/laravel-model-status

Laravel package to assign statuses to Eloquent Models

1.0.2 2021-02-11 12:53 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:56 UTC


README

Assign statuses to your Eloquent Model.

styleci.io

Installation

Install package via composer:

composer require fabpl/laravel-model-status

Run install artisan command:

php artisan model-status:install

Optionally you can publish config files with:

php artisan model-status:publish

Migrate the statuses table:

php artisan migrate

Usage

Add the HasStatuses trait to a model and defines available status.

use Fabpl\ModelStatus\HasStatuses;

class Post extends Model
{
    use HasStatuses;
    
    /**
     * Get available status list.
     *
     * @return array
     */
    public function getAvailableStatus(): array
    {
        return ['draft', 'published', 'archived'];
    }
}

Set a new status

You can set a new status like this:

$postModel->setStatus('published');

Retrieving statuses

You can get the current status name of model:

$postModel->status;

You can list assigned status:

$postModel->statuses;

You can deal with this relation like this:

$postModel->statuses()->whereName('published')->get();

Changelog

Please see CHANGELOG.

Security

If you discover any security related issues, please email planchettefabrice at hotmail.com instead of using the issue tracker.

Credits