[英]Save custom timestamp column to db after the row has been created
I have a table in my db that includes a nullable timestamp column. 我的数据库中有一个表,其中包含可为空的时间戳列。
$table->timestamp('custom_timestamp')->nullable();
When the row in the db is created this column will be null. 创建数据库中的行后,此列将为空。 Later when the user does this specific thing I want to update this column with the timestamp when the user did this.
稍后,当用户执行此特定操作时,我想使用用户执行此操作时的时间戳来更新此列。
My question: What is the syntax for adding the custom timestamp to this column? 我的问题:将自定义时间戳添加到此列的语法是什么?
Pseudo code explanation: 伪代码说明:
$myObject->custom_timestamp = *AD THE CURRENT TIME*;
$myObject->save();
EDIT: If you downvote a seemingly legit question please explain your concerns in the comments 编辑:如果您不赞成看似合法的问题,请在评论中解释您的担忧
You can generate the timestamp like so: 您可以像这样生成时间戳:
Carbon 碳
use Carbon\Carbon;
$myObject->custom_timestamp = Carbon::now()->toDateTimeString();
Eloquent 雄辩
$myObject->custom_timestamp = $myObject->freshTimestampString();
You can use Carbon library for datetime related issue. 您可以将Carbon库用于与日期时间相关的问题。 Do the following if you want to set the current time of the user's action.
如果要设置用户操作的当前时间,请执行以下操作。
$object->custom_timestamp = Carbon::now();
$object->save();
Or 要么
$object->update(['custom_timestamp' => Carbon::now() ]);
To use Carbon in your code, add this line after <?php
要在代码中使用Carbon,请在
<?php
之后添加以下行
use Carbon\Carbon;
If Carbon is not already in your project add the library by running the following command: 如果您的项目中还没有Carbon,请通过运行以下命令添加库:
$ composer require nesbot/carbon
You don't need to use this library just for the task, but I really love this much powerful library and recommend. 您不必仅使用此库来完成任务,但是我真的很喜欢这个功能强大的库并推荐使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.