Tuesday, July 11, 2017

Laravel Eloquent update created_at value

Asked by: Morteza Negahi


I need to change a post created_at because i send sms to posts created at this month according to created_at. When i try with this created_at does not change value!

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

Answers

Answered by: Milad Teimoeri at 2017-07-11 05:11PM



try this

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


Answered by: Don't Panic at 2017-07-11 07:19PM



created_at is not typically mass assigned like that. You probably need to add it to the $fillable attribute on your Post model, eg:

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

Note that as someone else pointed out, Carbon::today() does not seem like the right thing to use - it should work, but gives you a time of midnight. You probably want Carbon::now(), if you really want an accurate timestamp.




Source

No comments:

Post a Comment

Adbox