timestamps field in mysql not working

WBOY
Release: 2023-03-01 15:16:02
Original
1359 people have browsed it

Why did I set a field type to timestamps, but it did not automatically fill in the current timestamp when updating or adding as I expected?

The same goes for the data table I generated using Laravel's migrate. The timestamps field in the generated table has no effect at all. what's going on?

This is the migration file

<code><?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEquip extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('equip', function (Blueprint $table) {
            // $table->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()
    {
        //
    }
}
</code>
Copy after login
Copy after login

Reply content:

Why did I set a field type to timestamps, but it did not automatically fill in the current timestamp when updating or adding as I expected?

The same goes for the data table I generated using Laravel's migrate. The timestamps field in the generated table has no effect at all. what's going on?

This is the migration file

<code><?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEquip extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('equip', function (Blueprint $table) {
            // $table->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()
    {
        //
    }
}
</code>
Copy after login
Copy after login

Have you set the timestamp field ON UPDATE CURRENT_TIMESTAMP?
The default value under laravel settings

<code> $table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));</code>
Copy after login

Thanks for the invitation.

By default, Eloquent will automatically maintain the created_at and updated_at fields of the database table. Just add these two "timestamp" fields to the database table and Eloquent will handle the rest. If you don’t want Eloquent to automatically maintain these fields, add the following attributes to the model class:

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

You are not using Model to perform database operations at all, and it has nothing to do with whether timestamps is set to CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

  1. Laravel is 5.1+

  2. Laravel recommends that the table name be plural, your table name should be equips

  3. Laravel requires a primary key, and the name is recommended to be id. Your primary key has been renamed

  4. Need a new Model file

<code>app/Equip.php</code>
Copy after login

This class will automatically point to the equipstable

<code>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';

}</code>
Copy after login

Operation

<code>$e = Equip::create([
    'eqiup_name' => 'wwww',
    'eqiup_desc' => 'xxxx',
]);

$e->update([
   'eqiup_name' => 'yyyy', 
]);</code>
Copy after login

If you do it like this, Laravel still has no automatic maintenancecreated_at updated_at
I will use my head as a stool for you to sit on. After all, I have been using Laravel for more than 2 years. If you don’t turn off timestamps, your situation will not happen.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template