0

Model

...
protected $dates = [
        'closed_at',
    ];
...

table info

...
$table->timestamp('closed_at');
...

Why the closed_at field is automatically updated when I modify the model ?

  • Write code for controller also – ashok poudel Sep 02 '20 at 19:06
  • I just run the code in Tinker, Like this `$post = Post::find(1); $post->title = 'new title'; $post->save();` – fengzi91 Sep 02 '20 at 19:09
  • That shouldn't happen . The $dates atrribute only cast it to carbon instance doesn't update it – ashok poudel Sep 02 '20 at 19:12
  • I ran into the same issue a while back: https://stackoverflow.com/questions/56045660/laravel-timestamp-being-updated-without-explicit-call-to-do-so. I think you've figured it out, but MySQL does some odd stuff with date fields. I think if you use `$table->timestamps()` in your migration, to add `created_at` and `updated_at`, you won't run into this problem, so something to consider in the future. That or include `->nullable()` on the migration. – Tim Lewis Sep 02 '20 at 19:49

2 Answers2

0

It will not take automatically. You have to insert it.

$post= Post::find(1);
$post->title= 'new title';
$post->closed_at=now();
$post->save();

For more information see documentation Or You can check stackoverflow answer for similar question.

Bappi Saha
  • 404
  • 4
  • 10
0

This is because $table->timestamp('field') is automatically maintained by mysql. Like this DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP