文档没有提及任何有关通过迁移更改数据类型的内容。
我的数据库表中有此列
$table->smallInteger('driverslicensetype')->nullable();
我正在保存四位数字,但是当数字以0(零)开头时,例如0230,它将在DB中保存为230。这是错误的。
现在我想将此列数据类型从smallInteger 更改为varchar。
我如何通过迁移来做到这一点?
最佳答案
要使用架构更改功能,您需要 Doctrine DBAL:
composer require doctrine/dbal
现在您可以使用change()
:
public function up()
{
Schema::table('foo', function (Blueprint $table) {
$table->string('driverlicensetype', 4)->nullable()->change();
});
}
public function down()
{
Schema::table('foo', function (Blueprint $table) {
$table->smallInteger('driverlicensetype')->nullable()->change();
});
}
如果您更喜欢原始方法(或者如果您的 SQL 限制性更强,例如当 driverlicensetype
是外键时),请使用 DB::statement
:
public function up()
{
DB::statement('ALTER TABLE foo ...');
}
// ...
关于Laravel - 如何更改列类型(smallInteger => string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141518/