How to Create Table using Migration in Laravel?

Raviya Technical
2 min readAug 24, 2021

--

Hello Developer,

In this example, I will show you how to create a database table using the migration command in laravel. We will look at examples of how to create a table through migration in laravel. we will help you to give an example of how to create table migration in laravel. you will learn laravel to create a table using migration. you will do the following things to create a table in laravel using migration.

I will guide you on how to create a database table using laravel migration. we will use the laravel command to creating migration for the table. you can easily create migration in laravel 6, laravel 7, and laravel 8.

I will also let you know how to run migration and rollback migration and how to create migration using the command in laravel. let’s see the below instruction.

Create Migration:

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

php artisan make:migration create_posts_table

After running the above command, you can see create new file as below and you have to add a new column for string, integer, timestamp, and text data type as like bellow:

Location:-database/migrations/2020_04_01_064006_create_posts_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('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 the below command we can run our migration and create a database table.

php artisan migrate

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

How to Create Table using Migration in Laravel?

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

Did you know you can clap multiple times? 🥰 If this story added value to your day, please show your support by giving it a 👏 clap, sharing it with others, or even sponsoring my work. Your appreciation means the world to me!

--

--

No responses yet