Download the PHP package spatie/laravel-model-status without Composer
On this page you can find all versions of the php package spatie/laravel-model-status. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spatie/laravel-model-status
More information about spatie/laravel-model-status
Files in spatie/laravel-model-status
Package laravel-model-status
Short Description A package to enable assigning statuses to Eloquent Models
License MIT
Homepage https://github.com/spatie/laravel-model-status
Informations about the package laravel-model-status
Assign statuses to Eloquent models
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.
This package provides a HasStatuses
trait that, once installed on a model, allows you to do things like this:
Support us
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.
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.
Installation
You can install the package via composer:
You must publish the migration with:
Migrate the statuses
table:
Optionally you can publish the config-file with:
This is the contents of the file which will be published at config/model-status.php
Usage
Add the HasStatuses
trait to a model you like to use statuses on.
Set a new status
You can set a new status like this:
A reason for the status change can be passed as a second argument.
Retrieving statuses
You can get the current status of model:
You can also get latest status of a given name:
Get all available status names for the model.
The following examples will return statusses of type status 1
or status 2
, whichever is latest.
All associated statuses of a model can be retrieved like this:
This will check if the model has status:
Retrieving models with a given latest state
The currentStatus
scope will return models that have a status with the given name.
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.
You can also provide an array of status names to exclude from the query.
Validating a status before setting it
You can add custom validation when setting a status by overwriting the isValidStatus
method:
If isValidStatus
returns false
a Spatie\ModelStatus\Exceptions\InvalidStatus
exception will be thrown.
You may bypass validation with the forceSetStatus
method:
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:
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:
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:
Delete single status from model:
Delete multiple statuses from model at once:
Events
TheSpatie\ModelStatus\Events\StatusUpdated
event will be dispatched when the status is updated.
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.
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.
Testing
This package contains integration tests that are powered by orchestral/testbench.
You can run all tests with:
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
Credits
- Thomas Verhelst
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.