Download the PHP package orobogenius/laravel-model-statable without Composer
On this page you can find all versions of the php package orobogenius/laravel-model-statable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download orobogenius/laravel-model-statable
More information about orobogenius/laravel-model-statable
Files in orobogenius/laravel-model-statable
Package laravel-model-statable
Short Description Add states to your laravel models.
License MIT
Homepage https://github.com/orobogenius/laravel-model-statable
Informations about the package laravel-model-statable
Laravel Model Statable
This package lets you add predefined states to your laravel models. This allows you to define states that can be applied to your models at various points in your application.
You can define all your states in one place and transition your model instances into different states. For example, you may define an
admin
state that transforms a user into an admin.
Contents
- Installation
- Usage
- Testing
- License
Installation
Composer
To install this package via composer, simply run:
or just add:
to your composer.json
file, then run composer install
or composer udpate
.
Usage
To add states to your laravel models, add the Orobogenius\Statable\Statable
trait to the model.
This trait lets you apply predefined states to your model instances. To tell the package about the states that can be applied to your models,
you first have to define them. States are defined as methods on your models. To define a state, prefix the model method with state
.
Defined states should always return an array that is keyed by attributes on your model.
Applying states
For states that has been defined on your model, you may call the states
method on your model instances and pass the state(s)
that should be applied to the model:
You can, of course, apply more than one state to a model by passing in an array of states to the states
method:
Closure attributes
You may add closure attributes to your model states definitions. The closure also receives the evaluated attributes of the enclosing states that define them:
Relationships
You may specify relationships and states to apply to those relationships in a model's state definition. This lets you to update related
models when you apply certain states to model instances. For example, setting all invoice items to processed
when an invoice is
in a paid state. To add relations to a model's state definition, add a with_relations
key to the array that is being returned from the
state defintion. The value should be an array that specifies the relationship as a key and the states to apply as the value:
Invoice
InvoiceItem
The value of the relationship in the model definition can also be an array of states to be applied to the related models.
Testing
License
MIT license (MIT) - Check out the License File for more.