lareval重命名created_at和updated_at字段
一、总结
一句话总结:
要改变created_at和updated_at的名称,模型和数据迁移里面都需要改变
在模型中指定数据类型之后,运行迁移文件created_at和updated_at的格式还是datatime格式,说明:php artisan migrate执行数据迁移的时候,并不会去找数据迁移所对应的模型
1、模型中如何修改created_at和updated_at的名称?
const CREATED_AT = 'to_created_at';
const UPDATED_AT = 'to_updated_at';
2、模型中如何修改created_at和updated_at的格式为int(10)格式?
protected $dateFormat = 'U';
#此属性决定插入和取出数据库的格式,默认datetime格式,'U'是int(10)
protected $dateFormat = 'U';
//如果不设置,无论存储格式是datetime还是int,则取出时是Carbon对象
//Carbon对象返给前端时自动变为datetime字符串,后端谨慎处理
二、lareval重命名created_at和updated_at字段
1、模型
1 <?php 2 3 namespace AppModelAdmin; 4 5 use IlluminateDatabaseEloquentModel; 6 7 class TestOrm extends Model 8 { 9 // 10 protected $primaryKey='to_id'; 11 protected $guarded=[]; 12 13 14 const CREATED_AT = 'to_created_at'; 15 const UPDATED_AT = 'to_updated_at'; 16 17 #此属性决定插入和取出数据库的格式,默认datetime格式,'U'是int(10) 18 protected $dateFormat = 'U'; 19 //如果不设置,无论存储格式是datetime还是int,则取出时是Carbon对象 20 //Carbon对象返给前端时自动变为datetime字符串,后端谨慎处理 21 }
2、数据迁移
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateTestOrmsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('test_orms', function (Blueprint $table) { $table->increments('id'); $table->integer('to_created_at'); $table->integer('to_updated_at'); //$table->timestamps(); //用timestamps的话字段名和字段格式都会不正确 //php artisan migrate执行数据迁移的时候,并不会去找数据迁移所对应的模型 }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('test_orms'); } }
用$table->timestamps();的话:
用timestamps的话字段名和字段格式都会不正确
模型中指定了格式,格式错误说明:php artisan migrate执行数据迁移的时候,并不会去找数据迁移所对应的模型
3、数据库
4、控制器中操作
存 $ans=TestOrm::create([]); 取 $ans=TestOrm::get(); dd($ans);