Create database migrations with foreign keys and indexes
✓Works with OpenClaudeYou 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:statusto see existing migrations - Verify your database connection in
.envfile (DB_CONNECTION, DB_HOST, DB_DATABASE) - Check that
database/migrationsdirectory exists in your Laravel project root
Steps
- Generate a new migration file using
php artisan make:migration create_users_table --create=users(use--createfor new tables or--tablefor modifications) - Open the generated migration file in
database/migrations/YYYY_MM_DD_HHMMSS_create_users_table.php - In the
up()method, define columns with$table->id()for primary keys, then add other columns with appropriate types (string(),integer(),text(),timestamp(), etc.) - Add foreign key constraints using
$table->foreignId('role_id')->constrained('roles')->onDelete('cascade')which automatically creates the foreign key relationship - Add indexes with
$table->index(['column_name'])for frequently queried columns or$table->unique(['email'])for unique constraints - Define the
down()method with$table->dropIfExists('users')to allow rollback - Run
php artisan migrateto execute the migration and create the table in your database - Verify the table structure with
php artisan tinkerand 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
Related Laravel Skills
Other Claude Code skills in the same category — free to download.
Laravel Setup
Scaffold Laravel project with authentication and API
Laravel Eloquent
Write Eloquent models with relationships and scopes
Laravel API Resource
Build REST API with resources and form requests
Laravel Queue
Set up job queues with Redis/database drivers
Laravel Testing
Write feature and unit tests with PHPUnit and Pest
Laravel Livewire
Build reactive components with Laravel Livewire
Laravel Nova
Customize Laravel Nova admin panels
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.