laravel迁移:使用storedas()(生成列)和subtime()mysql函数

wqlqzqxt  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(461)

这是我的 up() 迁移中的方法

$table->dateTimeTz('from');
$table->time('expire_by_time')->default('0:30:00');

$expression = 'subtime(\'from\', \'expire_by_time\')';
$table->dateTimeTz('expire_at')->storedAs($expression);

我的活动始于 from 但我希望它在开始前30分钟(或一小时,或50小时…)就“过期”。稍后在我的代码中,我可以使用这个新生成的 expire_at 用于显示或排序的列。
然而,这是不工作的一些原因,我不断得到 0000-00-00 00:00:00 为了 expired_at

+-------+---------------------+----------------+---------------------+
| id    | from                | expire_by_time | expire_at           |
+-------+---------------------+----------------+---------------------+
| 11111 | 2019-09-29 12:00:00 | 00:30:00       | 0000-00-00 00:00:00 |
+-------+---------------------+----------------+---------------------+

我在这里做错什么了?

jk9hmnmh

jk9hmnmh1#

我不知道如何逃避角色。这是:

$expression = 'subtime(\'from\', \'expire_by_time\')';

应该是这样的:

$expression = 'subtime(`from`, `expire_by_time`)';

现在我明白了:

+-------+---------------------+----------------+---------------------+
| id    | from                | expire_by_time | expire_at           |
+-------+---------------------+----------------+---------------------+
| 11111 | 2019-09-29 12:00:00 | 00:30:00       | 2019-09-29 11:30:00 |
+-------+---------------------+----------------+---------------------+

相关问题