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();
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.
You can install the package via composer:
composer require spatie/laravel-model-states
You can publish the config file with:
php artisan vendor:publish --provider="Spatie\ModelStates\ModelStatesServiceProvider" --tag="model-states-config"
This is the content of the published config file:
return [
/*
* The fully qualified class name of the default transition.
*/
'default_transition' => Spatie\ModelStates\DefaultTransition::class,
];
Please refer to the docs to learn how to use this package.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
总结laravel-admin展示用到的基本方法 基础用法 自定义model 当列表数据获取有特定条件或自己写ORM方法时可以用到,支持排序 $grid->model()->select('id','name')->where('status',1)->groupBy('project_id'); $grid->model()->select('id','name')->where('status
Laravel-admin文档地址:http://laravel-admin.org/docs/zh/installation 按照文档安装 如果出现引用错误或者No application encryption key has been specified参照部署篇:https://blog.csdn.net/qq_24910011/article/details/96283768 mysql版
1.添加菜单,指定路径 2.定义路由 routes.php $router->group(['prefix' => 'test'], function ($router) { $router->get('', TestController::class.'@index'); //列表 $router->get('create', TestController::cl
我的个人博客:逐步前行STEP 项目中有一个商品表(production) ,有一个库存表(repertory),两者一对一关系,production有发布字段(release),需求是在repertory的grid中,有一个switch开关,用于发布production。 如果直接使用 g r i d − > c o l u m n ( ′ p r o
关联关系 * 一对一 ```php // 主键在A中,A模型代码: public function user() { return $this->belongsTo('App\Models\User', 'user_id', 'id'); } // A的控制器中,注意: 外键user_id $form->select('user_id', '用户
我的个人博客:逐步前行STEP 我把一个字段设默认值为0 需要审核操作 通过设为1 不通过设为2 $states = [ 'on' => ['value' => 1, 'text' => '通过', 'color' => 'success'], 'off' => ['value' => 2, 'text' => '不通过', 'co
model 设置软删除配置 class Demo extends Model{ use SoftDeletes; $datas = ['deleted_at']; #格式化 public function getStartTimeAttribute($value) { return !empty($value)? date('Y-m-d H:i:s', $value):''; } p
生成填充器 要生成一个填充器,可以通过 Artisan 命令 make:seeder。所有框架生成的填充器都位于 database/seeds 目录: php artisan make:seeder StudentsTableSeeder 编辑填充器 <?php use Illuminate\Database\Seeder; use App\Http\Models\Student; class
layout title date author desc in_head post Laravel的50个技巧 2017-12-07 15:50:02 +0800 南丞 Laravel框架使用的50个技巧 <style> .article-content p{ text-indent: 2em; line-height: 1.75rem; }</style> Laravel的50个技巧 Eloq
如果你想为你的 Laravel 项目写一些测试,那么你可能需要在某个时候编写一些工厂模式。 当我第一次听到工厂一词时,我不知道它的含义和作用,更不用说了解它们可以为你的测试带来的好处了。 假设你有一个产品 Controller,该控制器具有一种存储方法来保存新产品的详细信息。 产品可能具有产品代码,标题,价格,描述和标签等属性,这些都在请求中发送到 store 方法。 我的官方群点击此处。 如果你
Model Caching for Laravel Supporting This Package This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this,
本文向大家介绍laravel model 两表联查示例,包括了laravel model 两表联查示例的使用技巧和注意事项,需要的朋友参考一下 1对一 定义一对一关联 一对一关联是很基本的关联。例如一个 User 模型会对应到一个 Phone 。 在 Eloquent 里可以像下面这样定义关联: 传到 hasOne 方法里的第一个参数是关联模型的类名称。定义好关联之后,就可以使用 Eloquent
获取模型 get 函数 public function get($columns = ['*']) { $builder = $this->applyScopes(); if (count($models = $builder->getModels($columns)) > 0) { $models = $builder->eagerLoadRelations($m
前言 前面几个博客向大家介绍了查询构造器的原理与源码,然而查询构造器更多是为 Eloquent Model 服务的,我们对数据库操作更加方便的是使用 Eloquent Model。 本篇文章将会大家介绍 Model 的一些特性原理。 Eloquent Model 修改器 当我们在 Eloquent 模型实例中设置某些属性值的时候,修改器允许对 Eloquent 属性值进行格式化。如果对修改器不熟悉
前言 在前两篇文章中,向大家介绍了定义关联关系的源码,还有基于关联关系的关联模型加载与查询的源码分析,本文开始介绍第三部分,如何利用关联关系来更新插入关联模型。 hasOne/hasMany/MorphOne/MorphMany 更新与插入 save 方法 正向的一对一、一对多关联保存方法用于对子模型设置外键值: public function save(Model $model) { $
前言 数据库表通常相互关联。laravel 中的模型关联功能使得关于数据库的关联代码变得更加简单,更加优雅。本文会详细说说关于模型关联的源码,以便更好的理解和使用关联模型。官方文档:Eloquent:关联 定义关联 所谓的定义关联,就是在一个 Model 中定义一个关联函数,我们利用这个关联函数去操作另外一个 Model,例如,user 表是用户表,posts 是用户发的文章,一个用户可以发表多篇