The timestamps field in MySQL does not work

Source: Internet
Author: User
Why did I set a field type of timestamps, but he didn't automatically populate the current timestamp as I expected when updating or adding?

And the same as the data tables I generated with Laravel's migrate, the timestamps field in the generated table doesn't work at all. What's going on?

This is the migration file


  
   increments('id');            $table->smallInteger('equip_id')->unsigned()->comment('装备id');            $table->string('eqiup_name')->comment('装备名称');            $table->string('eqiup_desc')->comment('装备描述');            $table->timestamps();            $table->primary('equip_id');        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        //    }}

Reply content:

Why did I set a field type of timestamps, but he didn't automatically populate the current timestamp as I expected when updating or adding?

And the same as the data tables I generated with Laravel's migrate, the timestamps field in the generated table doesn't work at all. What's going on?

This is the migration file


  
   increments('id');            $table->smallInteger('equip_id')->unsigned()->comment('装备id');            $table->string('eqiup_name')->comment('装备名称');            $table->string('eqiup_desc')->comment('装备描述');            $table->timestamps();            $table->primary('equip_id');        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        //    }}

Did you set the timestamp field ON UPDATE CURRENT_TIMESTAMP ?
Laravel Setting the default value

 $table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

Thank you for your invitation.

The default eloquent automatically maintains database table created_at and Updated_at fields. Just add these two timestamp fields to the database table, and eloquent will handle the rest of the work. If you do not want eloquent to automatically maintain these fields, add the following attributes to the model class:

// 关闭自动更新时间戳class User extends Eloquent {    protected $table = 'users';    public $timestamps = false;}

You don't use the model to do the database operation, and whether the timestamps is a CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP setup-independent

    1. Laravel is 5.1+.

    2. Laravel is the suggestion that the table name is plural, and your table name should beequips

    3. Laravel requires a primary key, and the name is suggested as ID, your primary key changed name

    4. To a new model file

app/Equip.php

This class will automatically point to the equips table

use Illuminate\Database\Eloquent;class Equip extends Model{    //这3行是管理主键的    protected $primaryKey = 'equip_id';    public $incrementing = true; //主键是自增的    protected $keyType = 'int'; //主键类型        //如果要强制指定table名,取消注释下行,不然laravel会自动找equips表    //protected $table = 'equip';}

Operation

$e = Equip::create([    'eqiup_name' => 'wwww',    'eqiup_desc' => 'xxxx',]);$e->update([   'eqiup_name' => 'yyyy', ]);

If you do that, Laravel still doesn't have automatic maintenance created_at updated_at .
I put my head to you as a stool to sit, after all, I used laravel for more than 2 years, do not close timestamps, it is impossible to appear your situation

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.