In this tuto, i will show you how to create database table using migration command in laravel. 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 and laravel 7&8.
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:
To create a migration, use the make:migration
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 columns as like bellow:
database/migrations/2020_07_01_134514_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->id();
$table->string('title');
$table->text('content');
$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 'posts' table in your database.
Create Migration with Table
The --table
and --create
options may also be used to indicate the name of the table:
Example:
php artisan make:migration create_posts_table --table=articles
After that you can see created new 'articles' table in your database.
Run Specific Migration
php artisan migrate --path=/database/migrations/2020_07_01_134514_create_posts_table.php
Migration Rollback
php artisan migrate:rollback
Migration Reset
php artisan migrate:reset
The migrate:reset
command will roll back all of your application's migrations.
Roll Back & Migrate Using A Single Command
php artisan migrate:refresh
// Refresh the database and run all database seeds...
php artisan migrate:refresh --seed
Drop All Tables & Migrate
php artisan migrate:fresh
php artisan migrate:fresh --seed