我需要将$table->string('text');
的迁移列类型更改为文本类型,我尝试以几种方式执行此操作,但它们都不起作用。是否可以在一次迁移中完成。我可以猜测删除列然后再使用新类型创建它,但我想知道是否可以在一次迁移中执行此操作?
答案 0 :(得分:24)
您可以创建新的迁移和change just one column type:
public function up()
{
Schema::table('sometable', function (Blueprint $table) {
$table->text('text')->change();
});
}
您需要安装doctrine/dbal
才能使其正常工作
composer require doctrine/dbal
答案 1 :(得分:6)
你可以这样做
Schema::table('yourTable', function (Blueprint $table) {
$table->text('text')->change();
});
请务必将doctrine / dbal依赖项添加到您的composer.json文件
答案 2 :(得分:3)
可以使用TABLE迁移。
正如其他帖子中所述,请务必从项目根目录运行composer install doctrine/dbal
。
这些设置为:
php artisan make:migration alter_table_[yourtablenamehere]_change_[somecolumnname] --table=[yourtablenamehere]
来自项目根目录的。
来自文档:
https://laravel.com/docs/master/migrations#modifying-columns
class AlterTableSomething extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('table', function (Blueprint $table) {
$table->text('column_name')->change();
});
}
}
答案 3 :(得分:1)
如果使用change()
请求了未知的数据库类型枚举,Doctrine \ DBAL \ Platforms \ MySQL80Platform可能不支持它。
这意味着您的表中存在一些具有枚举类型的列(不一定更改为一列)。因此,可以使用以下功能代替使用change()
:
public function changeColumnType($table, $column, $newColumnType) {
DB::statement("ALTER TABLE $table CHANGE $column $column $newColumnType");
}
并像这样使用它:$this->changeColumnType('sometable','text','TEXT');
答案 4 :(得分:1)
以下为我工作。
您肯定需要安装 doctrine / dbal ,才能在终端中使用以下命令进行操作。
composer require doctrine/dbal
然后按照此处所述创建迁移-https://laravel.com/docs/master/migrations#generating-migrations
打开您的迁移文件并在下面写下。
Schema::table('yourTable', function (Blueprint $table) {
$table->string('column_name','4294967295')->change();
});
longText的最大字符数为4,294,967,295,Laravel会自动将column_name更改为longText数据类型。