Below is a full-blown, production-quality migration set that can power a Homzmart-like e-commerce back-end in Laravel 10+.
Cut, paste, and adapt at will 🤘
⚙️ 1. How to generate & organize
bash
php artisan make:migration create_roles_table
php artisan make:migration create_role_user_table
php artisan make:migration create_addresses_table
# …repeat for each file below
Put everything under database/migrations, keep the filenames in the same order (Laravel runs them chronologically).
<?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('roles', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->json('abilities')->nullable(); // fine-grained permissions
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('roles');
}
};
role_user (pivot)
php
return new class extends Migration {
public function up(): void
{
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('role_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->primary(['role_id', 'user_id']);
});
}
public function down(): void { Schema::dropIfExists('role_user'); }
};
return new class extends Migration {
public function up(): void
{
Schema::create('addresses', function (Blueprint $table) {
$table->id();
$table->morphs('addressable'); // user, order, etc.
$table->string('type')->default('shipping'); // shipping | billing
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('phone', 20)->nullable();
$table->string('email')->nullable();
$table->string('country', 100);
$table->string('state', 100)->nullable();
$table->string('city', 100);
$table->string('postcode', 20)->nullable();
$table->string('address_line1');
$table->string('address_line2')->nullable();
$table->json('meta')->nullable(); // lat/lng, notes …
$table->timestamps();
$table->softDeletes();
});
}
public function down(): void { Schema::dropIfExists('addresses'); }
};
Categories (adjacency list + depth)
php
return new class extends Migration {
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('parent_id')->nullable()
->constrained('categories')
->nullOnDelete();
$table->unsignedInteger('depth')->default(0);
$table->string('name');
$table->string('slug')->unique();
$table->longText('description')->nullable();
$table->boolean('is_active')->default(true);
$table->unsignedInteger('position')->default(0);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('categories'); }
};
Brands
php
return new class extends Migration {
public function up(): void
{
Schema::create('brands', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->longText('description')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('brands'); }
};
Products
php
return new class extends Migration {
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignId('brand_id')->nullable()->constrained()->nullOnDelete();
$table->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
$table->string('name');
$table->string('slug')->unique();
$table->string('sku')->unique()->nullable();
$table->longText('description')->nullable();
$table->decimal('price', 12, 2);
$table->decimal('compare_at_price', 12, 2)->nullable();
$table->decimal('cost_price', 12, 2)->nullable();
$table->boolean('track_stock')->default(true);
$table->unsignedInteger('default_stock')->default(0);
$table->boolean('is_active')->default(true);
$table->json('options')->nullable(); // Color, Size, Material etc.
$table->timestamps();
$table->softDeletes();
});
}
public function down(): void { Schema::dropIfExists('products'); }
};
Product Variants
php
return new class extends Migration {
public function up(): void
{
Schema::create('product_variants', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->string('sku')->unique();
$table->json('options'); // {"Color":"Red","Size":"XL"}
$table->decimal('price', 12, 2)->nullable();
$table->decimal('compare_at_price', 12, 2)->nullable();
$table->decimal('cost_price', 12, 2)->nullable();
$table->unsignedInteger('stock')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('product_variants'); }
};
Inventories (per warehouse, optional)
php
return new class extends Migration {
public function up(): void
{
Schema::create('inventories', function (Blueprint $table) {
$table->id();
$table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
$table->foreignId('warehouse_id')->nullable(); // If you have a warehouses table
$table->integer('qty')->default(0);
$table->integer('reserved')->default(0); // items locked in carts
$table->timestamps();
$table->unique(['product_variant_id','warehouse_id']);
});
}
public function down(): void { Schema::dropIfExists('inventories'); }
};
Carts & cart_items
php
// carts
return new class extends Migration {
public function up(): void
{
Schema::create('carts', function (Blueprint $table) {
$table->uuid('id')->primary(); // easier for guest carts
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
$table->string('currency', 3)->default('USD');
$table->decimal('grand_total', 12, 2)->default(0);
$table->json('meta')->nullable(); // coupon, device, etc.
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('carts'); }
};
// cart_items
return new class extends Migration {
public function up(): void
{
Schema::create('cart_items', function (Blueprint $table) {
$table->id();
$table->uuid('cart_id');
$table->foreign('cart_id')->references('id')->on('carts')->cascadeOnDelete();
$table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('qty')->default(1);
$table->decimal('price', 12, 2);
$table->timestamps();
$table->unique(['cart_id','product_variant_id']);
});
}
public function down(): void { Schema::dropIfExists('cart_items'); }
};
Orders
php
return new class extends Migration {
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->uuid('number')->unique(); // public-facing ID
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
$table->decimal('subtotal', 12, 2);
$table->decimal('discount_total', 12, 2)->default(0);
$table->decimal('shipping_total', 12, 2)->default(0);
$table->decimal('tax_total', 12, 2)->default(0);
$table->decimal('grand_total', 12, 2);
$table->string('currency', 3)->default('USD');
$table->string('status')->default('pending'); // pending|paid|shipped|completed|cancelled
$table->json('meta')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('orders'); }
};
Order items
php
return new class extends Migration {
public function up(): void
{
Schema::create('order_items', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
$table->string('name'); // copy for immutability
$table->json('options'); // Color:Red …
$table->unsignedInteger('qty');
$table->decimal('price', 12, 2);
$table->decimal('total', 12, 2);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('order_items'); }
};
Order addresses snapshot
php
return new class extends Migration {
public function up(): void
{
Schema::create('order_addresses', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained()->cascadeOnDelete();
$table->string('type'); // shipping|billing
// Copy of addresses columns
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('phone', 20)->nullable();
$table->string('email')->nullable();
$table->string('country', 100);
$table->string('state', 100)->nullable();
$table->string('city', 100);
$table->string('postcode', 20)->nullable();
$table->string('address_line1');
$table->string('address_line2')->nullable();
$table->json('meta')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('order_addresses'); }
};
Payments
php
return new class extends Migration {
public function up(): void
{
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained()->cascadeOnDelete();
$table->string('provider'); // stripe, tabby, cod …
$table->string('provider_payment_id')->nullable();
$table->decimal('amount', 12, 2);
$table->string('currency', 3)->default('USD');
$table->enum('status', ['pending','authorized','captured','failed','refunded'])->default('pending');
$table->json('payload')->nullable(); // gateway response
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('payments'); }
};
Shipments
php
return new class extends Migration {
public function up(): void
{
Schema::create('shipments', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained()->cascadeOnDelete();
$table->string('tracking_number')->nullable();
$table->string('carrier')->nullable(); // Aramex, DHL …
$table->string('status')->default('label_created'); // label_created|in_transit|delivered|returned
$table->json('meta')->nullable(); // events, weight …
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('shipments'); }
};
Coupons
php
return new class extends Migration {
public function up(): void
{
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->enum('type', ['fixed','percent'])->default('percent');
$table->decimal('value', 12, 2);
$table->unsignedInteger('usage_limit')->nullable();
$table->unsignedInteger('used')->default(0);
$table->dateTime('starts_at')->nullable();
$table->dateTime('expires_at')->nullable();
$table->boolean('is_active')->default(true);
$table->json('restrictions')->nullable(); // categories, min cart total …
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('coupons'); }
};
coupon_user (track redemptions)
php
return new class extends Migration {
public function up(): void
{
Schema::create('coupon_user', function (Blueprint $table) {
$table->id();
$table->foreignId('coupon_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('times_used')->default(0);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('coupon_user'); }
};
Reviews
php
return new class extends Migration {
public function up(): void
{
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
$table->tinyInteger('rating'); // 1-5
$table->text('title')->nullable();
$table->longText('body');
$table->boolean('is_approved')->default(false);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('reviews'); }
};
Wishlists
php
// wishlists (a user can have multiple wishlists)
return new class extends Migration {
public function up(): void
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name')->default('Default');
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('wishlists'); }
};
// wishlist_items
return new class extends Migration {
public function up(): void
{
Schema::create('wishlist_items', function (Blueprint $table) {
$table->id();
$table->foreignId('wishlist_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['wishlist_id','product_variant_id']);
});
}
public function down(): void { Schema::dropIfExists('wishlist_items'); }
};
Images (polymorphic)
php
return new class extends Migration {
public function up(): void
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->morphs('imageable'); // product, variant, category, brand, review etc.
$table->string('src');
$table->string('alt')->nullable();
$table->unsignedInteger('position')->default(0);
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('images'); }
};
🚀 4. Seeders & model relationships
Once the migrations are in place, craft factories & seeders (php artisan make:seeder BrandSeeder) and wire up Eloquent relationships, e.g.
php
// Product.php
public function variants() { return $this->hasMany(ProductVariant::class); }
public function images() { return $this->morphMany(Image::class,'imageable'); }
public function category() { return $this->belongsTo(Category::class); }
public function brand() { return $this->belongsTo(Brand::class); }
🧪 5. Test workflow
php artisan migrate:fresh --seed – rebuild DB
Hit /api/products (your controller) to make sure joins, eager loading, scopes all work.
That’s it! You now have a complete, scalable DB layer replicating the essentials of Homzmart.
Feel free to strip/add modules (loyalty points, returns, warehouse locations, VAT zones, etc.) as your project evolves.