All Projects → spatie → Laravel Model States

spatie / Laravel Model States

Licence: mit
State support for models

Projects that are alternatives of or similar to Laravel Model States

Laravel Mediable
Laravel-Mediable is a package for easily uploading and attaching media files to models with Laravel 5.
Stars: ✭ 541 (-3.22%)
Mutual labels:  eloquent, laravel
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (-24.15%)
Mutual labels:  eloquent, laravel
Laravel Eloquent Uuid
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
Stars: ✭ 388 (-30.59%)
Mutual labels:  eloquent, laravel
Corcel
Use WordPress backend with Laravel or any PHP application
Stars: ✭ 3,504 (+526.83%)
Mutual labels:  eloquent, laravel
Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (-10.91%)
Mutual labels:  eloquent, laravel
Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (-41.68%)
Mutual labels:  eloquent, laravel
Laravel Wallet
Easy work with virtual wallet
Stars: ✭ 401 (-28.26%)
Mutual labels:  eloquent, laravel
Laravel Translator
An Eloquent translator for Laravel
Stars: ✭ 275 (-50.81%)
Mutual labels:  eloquent, laravel
Squire
A library of static Eloquent models for common fixture data.
Stars: ✭ 496 (-11.27%)
Mutual labels:  eloquent, laravel
Laravel Moderation
A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.
Stars: ✭ 487 (-12.88%)
Mutual labels:  eloquent, laravel
Laravel Bookings
Rinvex Bookable is a generic resource booking system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Stars: ✭ 309 (-44.72%)
Mutual labels:  eloquent, laravel
Compoships
Multi-columns relationships for Laravel's Eloquent ORM
Stars: ✭ 537 (-3.94%)
Mutual labels:  eloquent, laravel
Laravel Attributes
Rinvex Attributable is a robust, intelligent, and integrated Entity-Attribute-Value model (EAV) implementation for Laravel Eloquent, with powerful underlying for managing entity attributes implicitly as relations with ease. It utilizes the power of Laravel Eloquent, with smooth and seamless integration.
Stars: ✭ 304 (-45.62%)
Mutual labels:  eloquent, laravel
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-32.92%)
Mutual labels:  eloquent, laravel
Decoy
A Laravel model-based CMS
Stars: ✭ 303 (-45.8%)
Mutual labels:  eloquent, laravel
Laravel Model Cleanup
Clean up unneeded records
Stars: ✭ 388 (-30.59%)
Mutual labels:  eloquent, laravel
Eloquent Builder
Provides an advanced filter for Laravel or Lumen model.
Stars: ✭ 264 (-52.77%)
Mutual labels:  eloquent, laravel
Laravel Eloquent Join
This package introduces the join magic for eloquent models and relations.
Stars: ✭ 270 (-51.7%)
Mutual labels:  eloquent, laravel
Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (+748.48%)
Mutual labels:  eloquent, laravel
Laravel Model Status
Easily add statuses to your models
Stars: ✭ 510 (-8.77%)
Mutual labels:  eloquent, laravel

Adding state behaviour to Eloquent models

Latest Version on Packagist Test Check & fix styling Total Downloads

This package adds state support to models. It combines concepts from the state pattern and state machines.

It is recommended that you're familiar with both patterns if you're going to use this package.

To give you a feel about how this package can be used, let's look at a quick example.

Imagine a model Payment, which has three possible states: Pending, Paid and Failed. This package allows you to represent each state as a separate class, handles serialization of states to the database behind the scenes, and allows for easy state transitions.

For the sake of our example, let's say that, depending on the state, the color of a payment should differ.

Here's what the Payment model would look like:

use Spatie\ModelStates\HasStates;

class Payment extends Model
{
    use HasStates;

    protected $casts = [
        'state' => PaymentState::class,
    ];
}

This is what the abstract PaymentState class would look like:

use Spatie\ModelStates\State;
use Spatie\ModelStates\StateConfig;

abstract class PaymentState extends State
{
    abstract public function color(): string;
    
    public static function config(): StateConfig
    {
        return parent::config()
            ->default(Pending::class)
            ->allowTransition(Pending::class, Paid::class)
            ->allowTransition(Pending::class, Failed::class)
        ;
    }
}

Here's a concrete implementation of one state, the Paid state:

class Paid extends PaymentState
{
    public function color(): string
    {
        return 'green';
    }
}

And here's how it is used:

$payment = Payment::find(1);

$payment->state->transitionTo(Paid::class);

echo $payment->state->color();

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:

composer require spatie/laravel-model-states

Usage

Please refer to the docs to learn how to use this package.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].