数据迁移 Migration 如何创建自定义字段
当前框架版本
Laravel Framework 9.52.10
问题叙述
这是目前迁移的使用方式
public function up(): void
{
Schema::create('company', function (Blueprint $table) {
$table->id();
$table->string('name', 50)->comment('公司名称')->default('');
$table->tinyText('remark')->nullable()->comment('备注');
$table->timestamp('created_at')->comment('created time')->useCurrent();
$table->timestamp('updated_at')->comment('updated time')->useCurrent()->useCurrentOnUpdate();
$table->timestamp('deleted_at')->nullable()->default(NULL)->comment('delete time');
$table->comment('公司表');
});
}
但是像 created_at
updated_at
deleted_at
这种字段每个表都存在。
请问能否可以自定义列格式 比如 扩充新 created_at
类型 对原来 $table->timestamp('created_at')->comment('created time')->useCurrent();
封装。
当然可以,Blueprint 对象是支持 “宏扩展的”,直接在 AppServiceProvider 的 boot 方法里面写就好了。