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
Laravel is 5.1+.
Laravel is the suggestion that the table name is plural, and your table name should beequips
Laravel requires a primary key, and the name is suggested as ID, your primary key changed name
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