假设我的MySQL数据库中已经有了一个表,我想在其中添加列。为此,我这样做:
Schema::table('mytable', function($table) {
$table->integer('my_special_integer')->after('previous_column');
$table->text('my_special_text')->after('my_special_integer');
$table->string('my_special_string')->after('my_special_text');
/*Some many other field*/
$table->string('my_last_column_to_add')->after('my_second_last_column');
}
如果我只想在某个前一列之后输入一堆列,是否会有一种不那么重复的方式?
答案 0 :(得分:0)
不,在()之后只应用sql AFTER sql语句。
ALTER TABLE table_name
ADD COLUMN new_column
AFTER existing_column
答案 1 :(得分:0)
没有办法开箱即用,但你可以做的是:
$columns = [
'my_special_integer' => 'integer',
'my_special_text' => 'text',
'my_special_string' => 'string',
'my_last_column_to_add' => 'string',
];
$previous = 'previous_column';
foreach ($columns as $name => $type) {
$table->$type($name)->after($previous);
$previous = $name;
}
希望这有帮助!
答案 2 :(得分:-1)
您可以使用mysql查询来改变迁移中的列DB::statement
,如下所示
public function up()
{
DB::statement("ALTER TABLE mytable MODIFY COLUMN column_name1 AFTER column_name2");
}
答案 3 :(得分:-1)
试试这个,希望它能帮助您找到正确的解决方案:
public function up()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}
public function down()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}