Reputation: 43
Laravel Framework version 5.2.5
When I updated a record, update_at was not changed but created_at changed to the current timestamp.
Is this correct?
MariaDB [moon]> show columns from users; +----------------+------------------+------+-----+---------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------------+------+-----+---------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO | UNI | NULL | | | password | varchar(60) | NO | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | | updated_at | timestamp | NO | | 0000-00-00 00:00:00 | | +----------------+------------------+------+-----+---------------------+-----------------------------+
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
Upvotes: 2
Views: 2884
Reputation: 1
hi guys my name is arash laghaei from iran i fix this problem please do it: vendor/laravel/framework/srs/illuminate/database/schema/blueprint.php line:792
public function timestamps()
{
$this->timestamp('updated_at');
$this->timestamp('created_at')->useCurrent();
}
please do it exactly i write. and fixed this problem.
Upvotes: 0
Reputation: 15911
A lot of people have had this issue recently, and you can read the discussion on Github here: https://github.com/laravel/framework/issues/11518
MySQL 5.7 no longer allows 0000-00-00 as a valid timestamp with strict mode turned on (which it is by default). So either use
->nullableTimestamps()
or->timestamp()->useCurrent()
.
You can fix this by changing this:
$table->timestamps();
To either one of these options:
// Option 1:
$table->nullableTimestamps();
// Option 2:
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();
Also, on this MySQL page: https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Alternatively, if
explicit_defaults_for_timestamp
is disabled (the default), do either of the following:Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.
In either case, the above solution should fix your problem.
Upvotes: 2
Reputation: 111829
Looking at your database structure the problem is on update CURRENT_TIMESTAMP
for created_at
column. It shouldn't be obviously like this. Looking at migrations it's unlikely this migration set automatically this, so probably it has been set manually in MySQL, so you should remove on update CURRENT_TIMESTAMP
for created_at
column.
When you update your model using Eloquent updated_at
column should be updated without a problem (also in current database schema) - if it's not you probably don't use Eloquent, so it won't be updated automatically. If you are not sure, please show your code how you update this record
Upvotes: 0