Migration Example
Migration Example
This is example migration. You need to modify it to fit your application's requirements.
Create Migration Product Table
Create migration product table example .
bash
php artisan make:model Product -mView Migration Product Table
View the migration example in the app/database/migrations/create_products_table.php file.
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('slug')->nullable();
$table->string('description')->nullable();
$table->string('sku')->nullable();
$table->string('image')->nullable();
$table->text('price')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
Create Migration Order Table
Create migration order table example .
bash
php artisan make:model Order -mView Migration Order Table
View the migration example in the app/database/migrations/create_orders_table.php file.
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->string('transaction_id')->unique()->nullable();
$table->string('validation_id')->unique()->nullable();
$table->string('customer_name')->nullable();
$table->string('customer_email')->nullable();
$table->string('customer_phone')->nullable();
$table->text('customer_address')->nullable();
$table->string('customer_city')->nullable();
$table->string('customer_country')->nullable();
$table->string('customer_postcode')->nullable();
$table->string('product_name')->nullable();
$table->integer('quantity')->nullable();
$table->decimal('total_amount', 10, 2)->nullable();
$table->string('store_id')->nullable();
$table->string('verify_sign_sha2')->nullable();
$table->string('bank_transaction_id')->nullable();
$table->string('card_type')->nullable();
$table->string('card_brand')->nullable();
$table->string('card_issuer')->nullable();
$table->string('card_issuer_country')->nullable();
$table->string('currency')->nullable();
$table->string('store_amount')->nullable();
$table->string('verify_sign')->nullable();
$table->string('payment_date')->nullable();
$table->string('tran_date')->nullable();
$table->string('cart_status')->nullable();
$table->enum('status', [
'pending',
'completed',
'failed',
'cancelled',
'refunded'
])->default('pending')->nullable();
$table->timestamps();
$table->index('transaction_id');
$table->index('status');
});
}
public function down()
{
Schema::dropIfExists('orders');
}
};
Create Migration Order Item Table
Create migration order item table example .
bash
php artisan make:model OrderItem -mView Migration Order Item Table
View the migration example in the app/database/migrations/create_order_items_table.php file.
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('order_items', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained()->onDelete('cascade');
$table->foreignId('product_id')->nullable()->constrained()->onDelete('set null');
$table->string('product_name');
$table->string('product_sku')->nullable();
$table->string('product_image')->nullable();
$table->decimal('unit_price', 10, 2);
$table->integer('quantity');
$table->decimal('total_price', 10, 2);
$table->decimal('discount', 10, 2)->default(0);
$table->decimal('tax', 10, 2)->default(0);
$table->json('attributes')->nullable();
$table->text('notes')->nullable();
$table->timestamps();
// Indexes
$table->index('order_id');
$table->index('product_id');
});
}
public function down()
{
Schema::dropIfExists('order_items');
}
};