陈泽宇 陈泽宇 - 4 years ago 462
SQL Question

Is laravel created_at and updated_at inverse?

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');
}
}

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download