Why does a field set to timestamps, but it does not automatically fill in the current timestamp during update or addition as I expected? In addition, the data table generated using migrate of Laravel is the same. the timestamps field in the generated table does not work at all. Yes... why does a field set to timestamps, but it does not automatically fill in the current timestamp during update or addition as I expected?
In addition, the data table generated using migrate of Laravel is the same. the timestamps field in the generated table does not work at all. Why?
This is the migration file.
Increments ('id'); $ table-> smallInteger ('Equip _ id')-> unsigned ()-> comment ('device ID '); $ table-> string ('eqiup _ name')-> comment ('device name'); $ table-> string ('eqiup _ desc ') -> comment ('device description'); $ table-> timestamps (); $ table-> primary ('Equip _ id ');});} /*** Reverse the migrations. ** @ return void */public function down (){//}}
Reply content:
Why does a field set to timestamps, but it does not automatically fill in the current timestamp during update or addition as I expected?
In addition, the data table generated using migrate of Laravel is the same. the timestamps field in the generated table does not work at all. Why?
This is the migration file.
Increments ('id'); $ table-> smallInteger ('Equip _ id')-> unsigned ()-> comment ('device ID '); $ table-> string ('eqiup _ name')-> comment ('device name'); $ table-> string ('eqiup _ desc ') -> comment ('device description'); $ table-> timestamps (); $ table-> primary ('Equip _ id ');});} /*** Reverse the migrations. ** @ return void */public function down (){//}}
You set the timestamp fieldON UPDATE CURRENT_TIMESTAMP
?
Set the default value for laravel.
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
Thank you.
By default, Eloquent automatically maintainscreated_at
And updated_at. You only need to add the two "timestamp" fields to the database table, and Eloquent will process the remaining work. If you do not want Eloquent to automatically maintain these fields, add the following attributes to the model class:
// Disable the automatic update timestamp class User extends Eloquent {protected $ table = 'users'; public $ timestamps = false ;}
You didn't use the Model to perform database operations at all, or whether timestamps isCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Settings are irrelevant.
Laravel is 5.1 +
Laravel recommends that the table name be a complex number. your table name should beequips
Laravel requires a primary key, and the name is recommended to be id. your primary key has changed its name.
To create a new Model file
app/Equip.php
This class automatically pointsequips
Table
Use Illuminate \ Database \ Eloquent; class Equip extends Model {// these three rows are the protected $ primaryKey = 'Equip _ id' of the primary key management; public $ incrementing = true; // the primary key is an auto-incrementing protected $ keyType = 'int'; // primary key type // If you want to forcibly specify the table name, uncomment the downstream key, otherwise laravel will automatically find the equips table // protected $ table = 'equip ';}
Operation
$e = Equip::create([ 'eqiup_name' => 'wwww', 'eqiup_desc' => 'xxxx',]);$e->update([ 'eqiup_name' => 'yyyy', ]);
If this is the case, Laravel still does not have automatic maintenance.created_at
updated_at
I'll give you my head as a stool. after all, I 've been using Laravel for more than two years. it's impossible to stop timestamps.