![](/img/trans.png)
[英]Update column automatically based on value of another column (same table) in Laravel (7)
[英]How to store value in column depending on another column in same table in Laravel 8
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('products_id');
$table->integer('stack');
$table->boolean('state')->default(0)->change();
$table->timestamps();
});
如何使列state检查堆栈
如果堆栈值大于 1 ( > 0 )堆栈获取值 ( 1 ) 否则其值仍为默认值 ( 0 )
在您的product
model 中,您可以侦听创建事件并将此逻辑添加到其中,就像这样
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 模型调度多个事件,允许您连接到模型生命周期中的以下时刻:检索、创建、创建、更新、更新、保存、保存、删除、删除、恢复、恢复和复制
有关此的更多信息,请查看此处的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.