How to Create Migration in Laravel 9?

This article will provide an example of how to create database table using migration command in laravel 9. I’m going to show you how to create migration in laravel 9. In this article, we will implement a how to create table migration in laravel 9. This post will give you a simple example of laravel 9 creating a table using migration.

I will guide you how to create database table using laravel migration. we will use laravel 9 command to creating migration for table.

In this example, we will simple create “posts” table with id, title, body, is_publish, created_at and updated_at columns. we will generate new migration by laravel 9 command and add above rows. Then we will run migration and we will get table created on mysql database. so let’s see bellow tutorial.

How to Create Migration in Laravel 9?

Create Migration:

Using bellow command you can simply create migration for database table.

php artisan make:migration create_posts_table

After run above command, you can see created new file as bellow and you have to add new column for string, integer, timestamp and text data type as like bellow:

database/migrations/2022_02_17_133331_create_posts_table.php

<?php

use IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateSupportFacadesSchema;

return new class extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->id();

$table->string('title');

$table->text('body');

$table->boolean('is_publish')->default(0);

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

};

Run Migration:

Using bellow command we can run our migration and create database table.

php artisan migrate

After that you can see created new table in your database as like bellow:

How to Create Migration in Laravel 9?

Create Migration with Table:

php artisan make:migration create_posts_table --table=posts

Run Specific Migration:

php artisan migrate --path=/database/migrations/2020_04_01_064006_create_posts_table.php

Migration Rollback:

php artisan migrate:rollback

I hope it can help you…

Leave a Comment