Laravel 插入未保存 created_at、updated_at 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50643765/
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
Laravel insert not saved created_at, updated_at columns
提问by Davit
I have Calendar model. I wont to insert mass data.
我有日历模型。我不会插入大量数据。
$data = [
[
'weekday' => 1,
'status' => 'active'
]
.......
];
$calendar->insert($data)
but in db created_at, and updated_at is null Why??;
但是在db created_at 中,updated_at 为null 为什么??
回答by Hassan Ali Salem
If you INSIST on using insert and to update timestamps automatically, Laravel does not offer this.
如果您坚持使用插入并自动更新时间戳,Laravel 不会提供此功能。
you need to do it manually from the code side. as someone mention in some answer as:
您需要从代码端手动完成。正如有人在某些答案中提到的那样:
'created_at' => $calendar->freshTimestamp(),
'updated_at' => $calendar->freshTimestamp()
I faced a similar problem in one project, so I wanted to insert but without adding the timestamps in my code every time I insert so. if you need to use insert without adding the timestamps manually. you can change your migrations file and add the timestamps manuallay:
我在一个项目中遇到了类似的问题,所以我想插入但没有在每次插入时在我的代码中添加时间戳。如果您需要使用插入而不手动添加时间戳。您可以更改迁移文件并手动添加时间戳:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'));
now the database will handle the created at and updated at on insert.
现在数据库将处理在插入时创建的和更新的。
the created_at will be equal to the updated_at and equal to the current timestamp
created_at 将等于 updated_at 并等于当前时间戳
then on save() laravel will handle updating the updated_at
然后在 save() laravel 将处理更新 updated_at
回答by Davit
One solution is this
一种解决方案是这样的
$data = [
[
'weekday' => 1,
'status' => 'active',
'created_at' => $calendar->freshTimestamp(),
'updated_at' => $calendar->freshTimestamp()
]
.......
];
$calendar->insert($data)
2 solution In Model
2 解决方案 Model
/**
*
*/
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = $model->freshTimestamp();
$model->updated_at = $model->freshTimestamp();
});
}
回答by Vishal Maru
Eloquent expects created_at and updated_at columns to exist on your tables.
Eloquent 期望 created_at 和 updated_at 列存在于您的表中。
If you want that these columns automatically managed by Eloquent, set the $timestamps property on your model to true and also add columns name into protected $fillable = []; array.
如果您希望 Eloquent 自动管理这些列,请将模型上的 $timestamps 属性设置为 true,并将列名称添加到 protected $fillable = []; 大批。
public $timestamps = true;
回答by Vishal Maru
Because you're inserting data, created_at and updated_at will only be automatically filled if you're using ->save()
.
因为您正在插入数据,所以仅当您使用->save()
.
Make sure you have added those fields to your protected $fillable = [];
array.
确保您已将这些字段添加到protected $fillable = [];
数组中。
You can also use $calendar->update($data)
or
您也可以使用$calendar->update($data)
或
$calendar->fill($data);
$calendar->save();
Hope I understood your question.
希望我理解你的问题。