2017-07-11 108 views
0

我需要更改文章created_at,因为我发送短信根据created_at在这个月创建的帖子。当我尝试这个created_at不会改变价值!Laravel雄辩更新created_at值

public function Controller(Posts $post){ 
    $post->update(['created_at'=>Carbon::today()]); 
} 
+0

好了,不说更新created_at - 例如尝试' - > update('created_at',Carbon :: now())'或类似。 –

+0

@JoelHinz不起作用 –

回答

0

试试这个

public function Controller(Posts $post){ 
     $post->created_at = Carbon::today(); 
     $post->save(['timestamps' => false]); 
} 
1

created_at通常不是mass assigned这样。你可能需要将其添加到$fillable属性您Post模型,例如:

protected $fillable = [...., 'created_at']; 

注意,因为别人指出,Carbon::today()似乎并不像正确的事情来使用 - 它应该工作,但给你一个午夜的时间。如果你真的想要一个准确的时间戳,你可能想要Carbon::now()

1

Carbon::today()实际上并没有生成有效的时间戳。您需要使用Carbon::today()->toDateTimeString()才能获得有效的时间戳。

更新片段:

public function Controller(Posts $post){ 
    $post->update(['created_at' => Carbon::today()->toDateTimeString()]); 
} 
+0

['Carbon :: today()'](http://carbon.nesbot.com/docs/)确实会生成有效的时间戳。 –