How to Change MySQL Table Engine in 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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Migrate SQL File in Laravel Migration?
- Laravel Migration Execute SQL Query Example
- Laravel Migration Remove Default Value Example
- Laravel Migration Change Datatype Timestamp to Datetime Example
- Laravel Migration Change Datatype Date to Datetime Example
- Laravel Migration Change Default Value Example
- Laravel Migration Change Datatype String to Integer Example
- How to Drop Soft Delete from Table using Laravel Migration?
- How to Create Migration in Laravel 9?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Enum Default Value Example
- How to Rollback Migration in Laravel?
- Laravel Migration Custom Index Name Example