migrations are used to manage database schema changes and version control. They provide a structured and convenient way to create, modify, and delete database tables and columns. Here are the reasons why we create migrations in Laravel:
Step1: Create Migration:
add below code in database/migrations/2014_10_12_000000_create_posts_table.php
<?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('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->boolean('is_publish')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Step 2: Run Migration:
Run Below Command for creating table
php artisan migrate
Create Migration with Table:
here, table name posts and migration name 2014_10_12_000000_create_posts_table.php
php artisan make:migration create_posts_table --table=posts
Run Specific Migration:
php artisan migrate --path=/database/migrations/2023_04_01_064006_create_posts_table.php
Migration Rollback:
php artisan migrate:rollback