How to Change MySQL Table Engine in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hello Artisan,

This tutorial aims to guide you through the process of changing the table engine in Laravel migrations. I will explain the steps to migrate a table from MyISAM to InnoDB using MySQL. By following this tutorial, you will gain a clear understanding of how to change the table engine in Laravel migrations.

In this example, i will give you three ways to change table engine in your laravel application.

1. Laravel Change Table Engine using Config

2. Laravel Change Table Engine using Migration

3. Laravel Change Table Engine using Migration with Alter

So, let's see the following ways to do that.

1. Laravel Change Table Engine using Config

Here, in our database.php config file we need to change mysql engine on following way:

config/database.php

<?php

use Illuminate\Support\Str;

return [

...

'connections' => [

'mysql' => [

....

'engine' => 'InnoDB',

]

]

]

2. Laravel Change Table Engine using Migration

You can change table engine for one table using migration as like the below:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::create('pages', function (Blueprint $table) {

$table->engine = "InnoDB";

$table->id();

$table->string('title');

$table->string('slug');

$table->text('body');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('pages');

}

};

3. Laravel Change Table Engine using Migration with Alter

You can change table engine for one table using migration as like the below:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

\DB::statement('ALTER TABLE pages ENGINE = InnoDB');

}

/**

* Reverse the migrations.

*/

public function down(): void

{

}

};

I hope it can help you...

Shares