Laravel在模型保存中两次插入差异
16lz
2021-01-22
I'm creating a Laravel project but I have an issue.
我正在创建一个Laravel项目,但我有一个问题。
I'm trying to save the difference between 2 timestamps to the database before I save the model:
我在保存模型之前尝试将两个时间戳之间的差异保存到数据库:
My table:
$table->increments('id');
$table->time('from');
$table->time('to');
$table->time('diff');
So.. before inserting, calculate the difference between these two times and insert this on the same time.
所以..在插入之前,计算这两次之间的差异并同时插入它。
Is this possible and can anyone help me?
这有可能,任何人都可以帮助我吗?
Thankyou,
2 个解决方案
#1
2
You can hook into saving() or updating() or creating()
你可以挂钩到save()或更新()或创建()
so in your model add one of the above depending on which one you want to hook (I think saving() if I understand you correctly) and implement sth like the following:
所以在你的模型中添加上面的一个取决于你想要挂钩的那个(我认为如果我理解正确的话,保存())并实现如下:
public static function boot()
{
parent::boot();
static::saving(
function ($table) {
$table->diff = strtotime($table->to) - strtotime($table->from);
}
);
}
更多相关文章
- MYSQL5.5和5.6参数的差异
- 从模型到控制器并返回到模型的数据
- 装机建项目vs2017和mysql5.7下建项目用EF建实体模型的过程..
- MySQL -如何在最小/最大日期差异超过3年的情况下选择id
- Sql查询性能限制行数之间的差异
- Netbeans6.1+JSF/VJSF/+JPA+MYSQL=酷炫快速开发模型
- Angular JS复选框 - 模型不默认为false
- Lua与javascript的差异
- three.js学习笔记 obj模型加载问题