0
0
Laravelframework~5 mins

Migration creation in Laravel

Choose your learning style9 modes available
Introduction

Migrations help you create and change database tables easily. They keep your database organized and shareable with your team.

When you want to create a new database table for your app.
When you need to add or remove columns from an existing table.
When you want to keep track of database changes in a safe way.
When working with a team to make sure everyone has the same database structure.
Syntax
Laravel
php artisan make:migration create_table_name --create=table_name

php artisan make:migration add_column_to_table_name --table=table_name

Use --create=table_name to make a new table migration.

Use --table=table_name to modify an existing table.

Examples
This creates a migration file to make a new users table.
Laravel
php artisan make:migration create_users_table --create=users
This creates a migration file to add a new column to the users table.
Laravel
php artisan make:migration add_email_to_users_table --table=users
Sample Program

This migration creates a new table called books with columns for id, title, author, and timestamps for created and updated times.

Laravel
<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('author');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('books');
    }
};
OutputSuccess
Important Notes

Run php artisan migrate to apply migrations to your database.

Use php artisan migrate:rollback to undo the last migration.

Always write the down method to safely reverse changes.

Summary

Migrations help manage database tables easily and safely.

Use php artisan make:migration with --create or --table to generate migration files.

Run migrations with php artisan migrate to update your database.