3

Does anybody know if there is a way to execute multiple Laravel alter table migration queries in one query?

For example:

Schema::table('table', function (Blueprint $table) {
    $table->integer('column 1');
    $table->integer('column 2');
});

This would create two ALTER table queries. Is there a way to make it do all in one query, other than writing SQL query.

Emir Hadzic
  • 63
  • 1
  • 6
  • I don't think this would be possible. I'm curious about why you want to do that though. Is there a performance benefit? – JamesG May 26 '17 at 12:55
  • Actually yes, I just did the testing. I created table with 10 milion records. Alter in one query takes slightly more time than one of the separate alter queries. – Emir Hadzic May 26 '17 at 13:11

1 Answers1

4

I realise this question is quite old now, but it wasn't answered.

I would say that the Laravel schema builder is more for convenience and cross platform compatibility, rather than for performance.

If you want to optimise ALTER queries, I would suggest using the DB facade and writing raw SQL.

Schema::table('table', function (Blueprint $table) {
    DB::statement('ALTER TABLE table DROP COLUMN a, DROP COLUMN a;');
});
fubar
  • 16,918
  • 4
  • 37
  • 43
  • How would that statement change if the table being altered wasn't under the default schema? – Adrian Lynch Aug 21 '19 at 18:28
  • 1
    @AdrianLynch - look at this question - https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel – fubar Aug 21 '19 at 21:30