Eloquent model not updating updated_at timestamp

后端 未结 5 645
無奈伤痛
無奈伤痛 2021-02-12 14:52

I\'m using Laravel 5.1. To make it simple, I have the following code

Migration:

Schema::create(\'sitemap_data\', function (Blueprint $table) {
    // Pri         


        
相关标签:
5条回答
  • 2021-02-12 14:56

    go to phpmyadmin and select your db and select the table tap on SQL tab above and type for example something like this:

    UPDATE `boshogriq_table` SET  `updated_at`= DATE_ADD(`created_at`, INTERVAL 1 MINUTE)
     WHERE  `updated_at` > DATE_ADD(`created_at`, INTERVAL 10 MINUTE)
    

    run it by go button. Laravel won't allow you do this

    0 讨论(0)
  • 2021-02-12 15:00

    As mentioned in comments, if the model did not change the timestamps wont be updated. However if you need to update them, or want to check if everything is working fine use $model->touch() - more here

    0 讨论(0)
  • 2021-02-12 15:01

    This is not the case in the question, but same issue will happen if you're using Query Builder instead of Eloquent Model.

    If you do

    DB::table('users')->where()->update()
    

    instead of

    User::where()->update()
    

    updated_at will not be updated.

    0 讨论(0)
  • 2021-02-12 15:06

    It is possible actually:

    $model->updated_at = Carbon::now();
    $model->save(['timestamps' => FALSE]);
    

    That will properly save the updated_at to now. If you're not sure if any of the model's columns have changed, but you do want to update the updated_at regardless - this is the way to go.

    0 讨论(0)
  • 2021-02-12 15:07

    Yeah what Musterknabe has done is correct but he should also check his model SitemapData.php it should have to have set $timestamps = true;

    <?php 
    
     class SitemapData extends Eloquent {
    
            public $timestamps = true;
    }
    
    0 讨论(0)
提交回复
热议问题