我在迁移(模式)和模型中添加了时间戳
$table->timestamps(); //In the Schema
public $timestamps = true; //In the model
我用数据库的所有字段创建了一个数组并插入到数据库中。
Checkin::insert($input);
在表中,它更新了除 updated_at、created_at 时间戳之外的所有字段
我有另一个(默认)用户模型,它更新时间戳。在该模型中,我使用 ->save() 方法
最佳答案
如果你想设置时间戳,你应该使用 create
而不是 insert
:
Checkin::create($input);
你也可以这样做
$checking = Checkin::create($input);
这将为您提供刚刚创建的签到。
通过将此添加到您的模型来确保字段可填写:
protected $fillable = array(
'name',
'field_1',
'field_2',
... // all the fields you want to be mass-assignable here
);
此外,如果您使用 Eloquent 模型,则无需设置 public $timestamps = true;
,因为这是 Laravel 中的默认值。
关于mysql - Model::insert() 没有更新时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087962/