$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
LaravelbeginnerNew

Laravel Migration

Share

Create database migrations with foreign keys and indexes

Works with OpenClaude

You are a Laravel database architect. The user wants to create database migrations with foreign keys and indexes to structure their database schema properly.

What to check first

  • Run php artisan migrate:status to see existing migrations
  • Verify your database connection in .env file (DB_CONNECTION, DB_HOST, DB_DATABASE)
  • Check that database/migrations directory exists in your Laravel project root

Steps

  1. Generate a new migration file using php artisan make:migration create_users_table --create=users (use --create for new tables or --table for modifications)
  2. Open the generated migration file in database/migrations/YYYY_MM_DD_HHMMSS_create_users_table.php
  3. In the up() method, define columns with $table->id() for primary keys, then add other columns with appropriate types (string(), integer(), text(), timestamp(), etc.)
  4. Add foreign key constraints using $table->foreignId('role_id')->constrained('roles')->onDelete('cascade') which automatically creates the foreign key relationship
  5. Add indexes with $table->index(['column_name']) for frequently queried columns or $table->unique(['email']) for unique constraints
  6. Define the down() method with $table->dropIfExists('users') to allow rollback
  7. Run php artisan migrate to execute the migration and create the table in your database
  8. Verify the table structure with php artisan tinker and query \DB::select('describe users')

Code

<?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('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('category_id')->constrained('categories')->onDelete('restrict');
            $table->string('title', 255);
            $table->text('content');
            $table->string('slug', 255)->unique();
            $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
            $table->integer('views')->default(0);
            $table->timestamps();
            
            // Indexes for query performance
            $table->index('user_id');
            $table->index('category_id');
            $table->index('slug');
            $table->index('status');
            $table->index(['created_at', 'status']);
        });
    }

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

Pitfalls

  • Foreign keys must reference an existing primary key column; if the parent table doesn't exist yet, create

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryLaravel
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
laravelmigrationdatabase

Install command:

curl -o ~/.claude/skills/laravel-migration.md https://clskills.in/skills/laravel/laravel-migration.md

Related Laravel Skills

Other Claude Code skills in the same category — free to download.

Want a Laravel skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.