2

I created such table in database.

Schema::create('packages', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('count'); $table->timestamp('expire_date'); $table->timestamps(); }); 

Now Imagine we have following records in database:

+----+-------+---------------------+---------------------+---------------------+ | id | count | expire_date | created_at | updated_at | +----+-------+---------------------+---------------------+---------------------+ | 1 | 120 | 2018-01-31 19:22:45 | 2018-01-29 17:12:01 | 2018-01-29 19:22:45 | | 4 | 140 | 2018-02-14 19:05:01 | 2018-01-29 17:20:16 | 2018-01-29 19:05:01 | | 27 | 10 | 2018-02-17 19:01:10 | 2018-01-29 19:05:01 | 2018-01-29 19:05:01 | +----+-------+---------------------+---------------------+---------------------+ 

when I try to edit a record using laravel eloquent:

$package = \App\Package::find(1); $package->count = 100; $package->save(); 

save method changes both expire_date and updated_at columns to current time!

+----+-------+---------------------+---------------------+---------------------+ | id | count | expire_date | created_at | updated_at | +----+-------+---------------------+---------------------+---------------------+ | 1 | 100 | 2018-01-29 19:32:24 | 2018-01-29 17:12:01 | 2018-01-29 19:32:24 | | 4 | 140 | 2018-02-14 19:05:01 | 2018-01-29 17:20:16 | 2018-01-29 19:05:01 | | 27 | 10 | 2018-02-17 19:01:10 | 2018-01-29 19:05:01 | 2018-01-29 19:05:01 | +----+-------+---------------------+---------------------+---------------------+ 

why expire_date value changes without even touching it?!!!

4
  • @TimLewis I'm asking why expire_date is changing. Commented Jan 29, 2018 at 16:12
  • Maybe you are using another package, and that package changes the field Commented Jan 29, 2018 at 16:26
  • 1
    @MahdiYounesi no, i found the reason. it's because of mysql Automatic Initialization and Updating for TIMESTAMP and DATETIME dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html Commented Jan 29, 2018 at 16:32
  • You should use Datetime for that 'expire_at' column instead. Commented Jan 29, 2018 at 16:54

2 Answers 2

2

Take a look your table DDL,it would be

expire_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, 

expire_date default value is current timestamp.
all you need do is, change ON UPDATE CURRENT_TIMESTAMP to null.

ALTER TABLE table ALTER COLUMN expire_date DEFAULT NULL 

or before you create:

https://github.com/laravel/framework/blob/be09eac00a2f750dc2d2209078cf61ae2eee85a1/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php#L647

https://github.com/laravel/framework/blob/d598a6630208b054376527e41848c106f4297b2b/src/Illuminate/Database/Schema/ColumnDefinition.php#L26

$table->timestamp('expire_date')->useCurrent(false); 
Sign up to request clarification or add additional context in comments.

Comments

1

From the docs:

The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value

So, Eloquent updates only the updated_at. If expire_date is also updated, it means Eloquent events are used in the app or something similar is happening (like DB updates it).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.