我需要更改文章created_at,因为我发送短信根据created_at在这个月创建的帖子。当我尝试这个created_at不会改变价值!Laravel雄辩更新created_at值
public function Controller(Posts $post){
$post->update(['created_at'=>Carbon::today()]);
}
我需要更改文章created_at,因为我发送短信根据created_at在这个月创建的帖子。当我尝试这个created_at不会改变价值!Laravel雄辩更新created_at值
public function Controller(Posts $post){
$post->update(['created_at'=>Carbon::today()]);
}
试试这个
public function Controller(Posts $post){
$post->created_at = Carbon::today();
$post->save(['timestamps' => false]);
}
created_at
通常不是mass assigned这样。你可能需要将其添加到$fillable
属性您Post
模型,例如:
protected $fillable = [...., 'created_at'];
注意,因为别人指出,Carbon::today()
似乎并不像正确的事情来使用 - 它应该工作,但给你一个午夜的时间。如果你真的想要一个准确的时间戳,你可能想要Carbon::now()
。
Carbon::today()
实际上并没有生成有效的时间戳。您需要使用Carbon::today()->toDateTimeString()
才能获得有效的时间戳。
更新片段:
public function Controller(Posts $post){
$post->update(['created_at' => Carbon::today()->toDateTimeString()]);
}
['Carbon :: today()'](http://carbon.nesbot.com/docs/)确实会生成有效的时间戳。 –
好了,不说更新created_at - 例如尝试' - > update('created_at',Carbon :: now())'或类似。 –
@JoelHinz不起作用 –