php 使用 Laravel 插入 created_at 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26506712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting created_at data with Laravel
提问by maxxon15
I can't seem to insert created_at
data in the database table with Laravel.
I'm trying to get that data from a POST and then trying to save it to the database.
我似乎无法created_at
使用 Laravel 在数据库表中插入数据。我正在尝试从 POST 中获取该数据,然后尝试将其保存到数据库中。
I'm currently doing like this:
我目前正在这样做:
$create_dt = date("Y-m-d H:i:s A", strtotime($_POST['post_date']." ".$_POST['post_time']));
$name = $_POST['name'];
$post = new Post();
$post->name = $name;
...
$post->created_at = $create_dt;
$post->save();
But it gives an error:
但它给出了一个错误:
Uncaught exception 'InvalidArgumentException' with message in Carbon.php
and
和
Unexpected data found. Unexpected data found. Data missing in Carbon.php
How can I solve this? Do I need to set $timestamps
in my Models to false? (I really don't want do that because I'm fine with how it automatically inserts the updated_at
)
我该如何解决这个问题?我需要$timestamps
在我的模型中设置为 false 吗?(我真的不想那样做,因为我对它自动插入 的方式感到满意updated_at
)
回答by Jerodev
In your User
model, add the following line in the User
class:
在您的User
模型中,在User
类中添加以下行:
public $timestamps = true;
Now, whenever you save or update a user, Laravel will automatically update the created_at
and updated_at
fields.
现在,每当您保存或更新用户时,Laravel 都会自动更新created_at
和updated_at
字段。
Update:
If you want to set the created at manually you should use the date format Y-m-d H:i:s
. The problem is that the format you have used is not the same as Laravel uses for the created_at
field.
更新:
如果您想手动设置创建时间,您应该使用日期格式Y-m-d H:i:s
。问题是您使用的格式与 Laravel 用于该created_at
字段的格式不同。
Update: Nov 2018 Laravel 5.6"message": "Access level to App\\Note::$timestamps must be public",
Make sure you have the proper access level as well. Laravel 5.6 is public
.
更新:2018 年 11 月 Laravel 5.6"message": "Access level to App\\Note::$timestamps must be public",
确保您也拥有正确的访问级别。Laravel 5.6 是public
.
回答by Sabrina Leggett
You can manually set this using Laravel, just remember to add 'created_at' to your $fillable array:
您可以使用 Laravel 手动设置它,只需记住将 'created_at' 添加到您的 $fillable 数组中:
protected $fillable = ['name', 'created_at'];
回答by eithed
Currently (Laravel 5.4) the way to achieve this is:
目前(Laravel 5.4)实现这一目标的方法是:
$model = new Model();
$model->created_at = Carbon::now();
$model->save(['timestamps' => false]);
回答by socm_
$data = array();
$data['created_at'] =new \DateTime();
DB::table('practice')->insert($data);
回答by Mike
In my case, I wanted to unit testthat users weren't able to verify their email addresses after 1 hour had passed, so I didn't want to do any of the other answers since they would also persist when not unit testing, so I ended up just manually updating the row after insert:
就我而言,我想对用户在 1 小时后无法验证他们的电子邮件地址进行单元测试,所以我不想做任何其他答案,因为它们在不进行单元测试时也会持续存在,所以我最终只是在插入后手动更新行:
// Create new user
$user = factory(User::class)->create();
// Add an email verification token to the
// email_verification_tokens table
$token = $user->generateNewEmailVerificationToken();
// Get the time 61 minutes ago
$created_at = (new Carbon())->subMinutes(61);
// Do the update
\DB::update(
'UPDATE email_verification_tokens SET created_at = ?',
[$created_at]
);
Note: For anything other than unit testing, I would look at the other answers here.
注意:对于单元测试以外的任何其他内容,我会在这里查看其他答案。