如何在Laravel 8中根据同一表中的另一列在列中存储值

  • 本文关键字:存储 一列 Laravel php laravel
  • 更新时间 :
  • 英文 :

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模型调度多个事件,使您能够在模型的生命周期中钩住以下时刻:检索创建已创建更新和更新保存恢复复制

有关此的更多信息,请查看此处的文档

最新更新