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);
        }
    );
}

更多相关文章

  1. MYSQL5.5和5.6参数的差异
  2. 从模型到控制器并返回到模型的数据
  3. 装机建项目vs2017和mysql5.7下建项目用EF建实体模型的过程..
  4. MySQL -如何在最小/最大日期差异超过3年的情况下选择id
  5. Sql查询性能限制行数之间的差异
  6. Netbeans6.1+JSF/VJSF/+JPA+MYSQL=酷炫快速开发模型
  7. Angular JS复选框 - 模型不默认为false
  8. Lua与javascript的差异
  9. three.js学习笔记 obj模型加载问题

随机推荐

  1. 学习python的第二十一天
  2. Python廖雪峰实战web开发(day4-编写Model)
  3. python之内存概念
  4. python笔记7:接口实现方法
  5. 见证历史!Python或将取代VBA,成为Excel官方
  6. django的视图和URL配置
  7. python 入门视频学习笔记+python入门视频
  8. 第二章 Python基本语法
  9. 【Python】Python3 字典 copy()方法
  10. 购物车程序练习