Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('products_id');
$table->integer('stack');
$table->boolean('state')->default(0)->change();
$table->timestamps();
});
如何使列状态检查堆栈
如果堆叠值大于1(>0(获取值(1
在您的product
模型中,您可以监听创建事件,并将此逻辑添加到其中,就像这个一样
public class Product extends Model {
//.....
protected static function booted()
{
parent::booted();
static::creating(function ($product) {
if($product->stack > 1){
$product->state = 1;
} else {
$product->state = 0;
}
});
}
// .....
}
还可以收听其他事件
Eloquent模型调度多个事件,使您能够在模型的生命周期中钩住以下时刻:检索,创建、已创建、更新和更新,保存,恢复,复制
有关此的更多信息,请查看此处的文档