0

After some actions inside of my Laravel 6 app, I'm trying to update my model object $last (class CurrentConditions). As you can see below, I'm trying to update update_time property to current timestamp. Except this property, I have another timestamp property inside the CurrentConditions model: external_update_time and I want this property to stay the same after updating and saving $last object. Problem is that external_update_time property is updated to current time after saving the model.

/** @var CurrentConditions $last */
$last = $this->getLastUpdatedCurrentConditions($cityID);
$last->update_time = Carbon::now("UTC");
$last->save();

Here is my model:

class CurrentConditions extends Model
{
    protected $table = 'current_conditions';

    ...

    public $timestamps = false;

    protected $dates = ['update_time', 'external_update_time'];

    ...
}

And here is my migration code:

Schema::create("current_conditions", function (Blueprint $table) {
      $table->bigIncrements("current_conditions_id");
      ...
      $table->timestamp("external_update_time");
      $table->timestamp("update_time")->useCurrent();
});

So why is external_update_time updating as well? Thanks for any help.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
P.N.
  • 341
  • 3
  • 16
  • how did you define these fields in your schema, migration? – lagbox Jan 02 '20 at 17:35
  • @lagbox Good point. I updated the question. – P.N. Jan 02 '20 at 17:38
  • and this is mysql isn't it? – lagbox Jan 02 '20 at 17:40
  • @lagbox Yes, it is, MariaDB 10.1. – P.N. Jan 02 '20 at 17:42
  • Does this answer your question? [Laravel Timestamp Being Updated Without Explicit Call To Do So](https://stackoverflow.com/questions/56045660/laravel-timestamp-being-updated-without-explicit-call-to-do-so) I faced this same issue a while back, and it happens when you have a non-nullable timestamp in your migrations; the first instance of said column will have an unexpected default update value. – Tim Lewis Jan 02 '20 at 17:50

2 Answers2

0

Please check that external_update_time is not set on Update to set CurrentTimeStamp

in the schema table design

AWS PS
  • 4,420
  • 1
  • 9
  • 22
0

This is how MySQL 5.7 works - first timestamp will be updated with current timestamp. You should modify order of columns in your migration (assuming it's not deployed yet to live server) to achieve what you want:

$table->timestamp("update_time")->useCurrent();
$table->timestamp("external_update_time");
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291