Laravel Migration Tips with Detailed Examples

By Maulik Paghdal

10 Dec, 2024

Laravel Migration Tips with Detailed Examples

Introduction

Laravel migrations provide a powerful way to manage your database schema. This guide will cover how to create migrations, change column names, modify column types, remove columns, and handle indexing and foreign keys with detailed examples.

1. Creating a Migration

To create a migration, use the make:migration artisan command.

php artisan make:migration create_users_table

This generates a migration file in the database/migrations directory. Inside the up method, define the table schema.

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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Run the migration:

php artisan migrate

2. Changing Column Name

To rename a column, you must use the renameColumn method provided by Laravel.

  1. Generate a new migration:
php artisan make:migration rename_user_name_column
  1. Edit the migration file:
Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('name', 'full_name');
});
  1. Run the migration:
php artisan migrate

3. Changing Column Type

Laravel uses the doctrine/dbal package to modify column types.

  1. Install the package:
composer require doctrine/dbal
  1. Create a migration:
php artisan make:migration change_user_name_column_type
  1. Modify the column type:
Schema::table('users', function (Blueprint $table) {
    $table->text('name')->change();
});
  1. Run the migration:
php artisan migrate

4. Removing a Column

To drop a column, use the dropColumn method.

  1. Generate a migration:
php artisan make:migration drop_user_column
  1. Remove the column:
Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('email_verified_at');
});
  1. Run the migration:
php artisan migrate

5. Adding Indexing

Indexes improve query performance. Laravel supports different types of indexes:

  • Unique Index
$table->string('email')->unique();
  • Composite Index
$table->index(['name', 'email']);
  • Dropping an Index
$table->dropIndex(['name_email_index']);

6. Foreign Keys

Foreign keys enforce relationships between tables.

  1. Add a foreign key:
Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
  1. Remove a foreign key:
Schema::table('posts', function (Blueprint $table) {
    $table->dropForeign(['user_id']);
    $table->dropColumn('user_id');
});

Summary Table

TaskCommandMethod
Create Migrationphp artisan make:migrationSchema::create
Rename Columnphp artisan make:migrationrenameColumn
Change Column Typecomposer require doctrine/dbalchange()
Remove Columnphp artisan make:migrationdropColumn
Add Index-unique(), index()
Foreign Keys-foreign(), dropForeign()

These tips will help you manage and modify your Laravel database schema effectively.