2017 © Pedro Peláez
 

library laravel-model-status

A package to enable assigning statuses to Eloquent Models

image

spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  • Friday, July 27, 2018
  • by Spatie
  • Repository
  • 9 Watchers
  • 219 Stars
  • 10,735 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 19 Forks
  • 0 Open issues
  • 18 Versions
  • 148 % Grown

The README.md

Assign statuses to Eloquent models

Latest Version on Packagist GitHub Workflow Status Check & fix styling Total Downloads, (*1)

Imagine you want to have an Eloquent model hold a status. It's easily solved by just adding a status field to that model and be done with it. But in case you need a history of status changes or need to store some extra info on why a status changed, just adding a single field won't cut it., (*2)

This package provides a HasStatuses trait that, once installed on a model, allows you to do things like this:, (*3)

// set a status
$model->setStatus('pending', 'needs verification');

// set another status
$model->setStatus('accepted');

// specify a reason
$model->setStatus('rejected', 'My rejection reason');

// get the current status
$model->status(); // returns an instance of \Spatie\ModelStatus\Status

// get the previous status
$latestPendingStatus = $model->latestStatus('pending');

$latestPendingStatus->reason; // returns 'needs verification'

Support us

, (*4)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*5)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*6)

Installation

You can install the package via composer:, (*7)

composer require spatie/laravel-model-status

You must publish the migration with:, (*8)

php artisan vendor:publish --provider="Spatie\ModelStatus\ModelStatusServiceProvider" --tag="migrations"

Migrate the statuses table:, (*9)

php artisan migrate

Optionally you can publish the config-file with:, (*10)

php artisan vendor:publish --provider="Spatie\ModelStatus\ModelStatusServiceProvider" --tag="config"

This is the contents of the file which will be published at config/model-status.php, (*11)

return [

    /*
     * The class name of the status model that holds all statuses.
     *
     * The model must be or extend `Spatie\ModelStatus\Status`.
     */
    'status_model' => Spatie\ModelStatus\Status::class,

    /*
     * The name of the column which holds the ID of the model related to the statuses.
     *
     * You can change this value if you have set a different name in the migration for the statuses table.
     */
    'model_primary_key_attribute' => 'model_id',

];

Usage

Add the HasStatuses trait to a model you like to use statuses on., (*12)

use Spatie\ModelStatus\HasStatuses;

class YourEloquentModel extends Model
{
    use HasStatuses;
}

Set a new status

You can set a new status like this:, (*13)

$model->setStatus('status-name');

A reason for the status change can be passed as a second argument., (*14)

$model->setStatus('status-name', 'optional reason');

Retrieving statuses

You can get the current status of model:, (*15)

$model->status; // returns a string with the name of the latest status

$model->status(); // returns the latest instance of `Spatie\ModelStatus\Status`

$model->latestStatus(); // equivalent to `$model->status()`

You can also get latest status of a given name:, (*16)

$model->latestStatus('pending'); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending`

Get all available status names for the model., (*17)

$statusNames = $model->getStatusNames(); // returns a collection of all available status names.

The following examples will return statusses of type status 1 or status 2, whichever is latest., (*18)

$lastStatus = $model->latestStatus(['status 1', 'status 2']);

// or alternatively...
$lastStatus = $model->latestStatus('status 1', 'status 2');

All associated statuses of a model can be retrieved like this:, (*19)

$allStatuses = $model->statuses;

This will check if the model has status:, (*20)

$model->setStatus('status1');

$isStatusExist = $model->hasStatus('status1'); // return true
$isStatusExist = $model->hasStatus('status2'); // return false

Retrieving models with a given latest state

The currentStatus scope will return models that have a status with the given name., (*21)

$allPendingModels = Model::currentStatus('pending');

//or array of statuses
$allPendingModels = Model::currentStatus(['pending', 'initiated']);
$allPendingModels = Model::currentStatus('pending', 'initiated');

Retrieving models without a given state

The otherCurrentStatus scope will return all models that do not have a status with the given name, including any model that does not have any statuses associated with them., (*22)

$allNonPendingModels = Model::otherCurrentStatus('pending');

You can also provide an array of status names to exclude from the query., (*23)

$allNonInitiatedOrPendingModels = Model::otherCurrentStatus(['initiated', 'pending']);

// or alternatively...
$allNonInitiatedOrPendingModels = Model::otherCurrentStatus('initiated', 'pending');

Validating a status before setting it

You can add custom validation when setting a status by overwriting the isValidStatus method:, (*24)

public function isValidStatus(string $name, ?string $reason = null): bool
{
    ...

    if (! $condition) {
        return false;
    }

    return true;
}

If isValidStatus returns false a Spatie\ModelStatus\Exceptions\InvalidStatus exception will be thrown., (*25)

You may bypass validation with the forceSetStatus method:, (*26)

$model->forceSetStatus('invalid-status-name');

Check if status has been assigned

You can check if a specific status has been set on the model at any time by using the hasEverHadStatus method:, (*27)

$model->hasEverHadStatus('status 1');

Check if status has never been assigned

You can check if a specific status has never been set on the model at any time by using the hasNeverHadStatus method:, (*28)

$model->hasNeverHadStatus('status 1');

Delete status from model

You can delete any given status that has been set on the model at any time by using the deleteStatus method:, (*29)

Delete single status from model:, (*30)

$model->deleteStatus('status 1');

Delete multiple statuses from model at once:, (*31)

$model->deleteStatus(['status 1', 'status 2']);

Events

TheSpatie\ModelStatus\Events\StatusUpdated event will be dispatched when the status is updated., (*32)

namespace Spatie\ModelStatus\Events;

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStatus\Status;

class StatusUpdated
{
    /** @var \Spatie\ModelStatus\Status|null */
    public $oldStatus;

    /** @var \Spatie\ModelStatus\Status */
    public $newStatus;

    /** @var \Illuminate\Database\Eloquent\Model */
    public $model;

    public function __construct(?Status $oldStatus, Status $newStatus, Model $model)
    {
        $this->oldStatus = $oldStatus;

        $this->newStatus = $newStatus;

        $this->model = $model;
    }
}

Custom model and migration

You can change the model used by specifying a class name in the status_model key of the model-status config file., (*33)

You can change the column name used in the status table (model_id by default) when using a custom migration where you changed that. In that case, simply change the model_primary_key_attribute key of the model-status config file., (*34)

Testing

This package contains integration tests that are powered by orchestral/testbench., (*35)

You can run all tests with:, (*36)

composer test

Changelog

Please see CHANGELOG for more information what has changed recently., (*37)

Contributing

Please see CONTRIBUTING for details., (*38)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*39)

Credits

License

The MIT License (MIT). Please see License File for more information., (*40)

The Versions

27/07 2018

dev-master

9999999-dev https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

27/07 2018

dev-analysis-XlJ9dw

dev-analysis-XlJ9dw https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

27/07 2018

1.6.1

1.6.1.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

19/07 2018

1.6.0

1.6.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

22/03 2018

1.5.0

1.5.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

20/03 2018

1.4.0

1.4.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

16/03 2018

1.3.1

1.3.1.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

14/03 2018

1.3.0

1.3.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

12/03 2018

1.2.0

1.2.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

02/03 2018

1.1.0

1.1.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

09/02 2018

1.0.6

1.0.6.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

09/02 2018

1.0.5

1.0.5.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

08/02 2018

1.0.4

1.0.4.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

07/02 2018

1.0.3

1.0.3.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

07/02 2018

1.0.2

1.0.2.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

07/02 2018

1.0.1

1.0.1.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

07/02 2018

1.0.0

1.0.0.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status

06/02 2018

0.0.1

0.0.1.0 https://github.com/spatie/laravel-model-status

A package to enable assigning statuses to Eloquent Models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-status