ItSolutionStuff.com

How to Change MySQL Table Engine in Laravel?

By Hardik Savani • November 5, 2023
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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Migrate SQL File in Laravel Migration?

Read Now →

Laravel Migration Execute SQL Query Example

Read Now →

Laravel Migration Remove Default Value Example

Read Now →

Laravel Migration Change Datatype Timestamp to Datetime Example

Read Now →

Laravel Migration Change Datatype Date to Datetime Example

Read Now →

Laravel Migration Change Default Value Example

Read Now →

Laravel Migration Change Datatype String to Integer Example

Read Now →

How to Drop Soft Delete from Table using Laravel Migration?

Read Now →

How to Create Migration in Laravel 9?

Read Now →

How to Update Enum Value in Laravel Migration?

Read Now →

Laravel Migration Enum Default Value Example

Read Now →

How to Rollback Migration in Laravel?

Read Now →

Laravel Migration Custom Index Name Example

Read Now →