How to Create Table using Migration in Laravel?
Hello Artisan,
In this example, i will show you how to create database table using migration command in laravel. We will look at example of how to create table through migration in laravel. we will help you to give example of how to create table migration in laravel. you will learn laravel create table using migration. you will do the following things for create table in laravel using migration.
I will guid you how to create database table using laravel migration. we will use laravel command to creating miration for table. you can easily create migration in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
I will also let you know how to run migration and rollback migration and how to create migration using command in laravel. let's see bellow instruction.
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/2020_04_01_064006_create_posts_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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 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:
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...
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 Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- Laravel Check If Foreach is Empty Example
- How to Get Table Name from Model in Laravel?
- How to Drop Foreign Key Constraint in Laravel Migration?
- How to Check Table Is Exists or Not in Laravel?
- How to Add MySQL Trigger from Migration in Laravel?