Laravel 11 E-Commerce - Create Model, Migration, and Relationship for Checkout
Welcome back to the Laravel E-Commerce Tutorial. In this video, we will learn about creating a model, migration, and relationship for checkout.
Step 1: Create Tables for Checkout
For checkout, we need four tables: Order, OrderItem, Address, and Transaction. Let's create these tables' models and migrations one by one.
Create Model and Migration for Order Table
In the command prompt, run the command:
php artisan make:model Order –m
Create Model and Migration for OrderItem Table
Now, create the model and migration for OrderItem:
php artisan make:model OrderItem –m
Create Model and Migration for Address Table
Next, create the model and migration for Address:
php artisan make:model Address –m
Create Model and Migration for Transaction Table
In last, create the model and migration for Transaction:
All four models and migrations have been created. Now, let's switch to the project.
Step 2: Define Columns and Relationships
Define Columns for Orders Table
Open the create_orders_table migration and add the following columns. Also, set the foreign key for the user_id column with the user table:
<?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('orders', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->decimal('subtotal');
$table->decimal('discount')->default(0);
$table->decimal('tax');
$table->decimal('total');
$table->string('name');
$table->string('phone');
$table->string('locality');
$table->text('address');
$table->string('city');
$table->string('state');
$table->string('country');
$table->string('landmark')->nullable();
$table->string('zip');
$table->string('type')->default('home');
$table->enum('status',['ordered','delivered','canceled'])->default('ordered');
$table->boolean('is_shipping_different')->default(false);
$table->date('delivered_date')->nullable();
$table->date('canceled_date')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('orders');
}
};
Define Columns for OrderItem Table
Now, open the create_order_item_migration and add these columns:
<?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('order_items', function (Blueprint $table) {
$table->id();
$table->bigInteger('product_id')->unsigned();
$table->bigInteger('order_id')->unsigned();
$table->decimal('price');
$table->integer('quantity');
$table->longText('options')->nullable();
$table->boolean('rstatus')->default(false);
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('order_items');
}
};
Define Columns for Address Table
Next, open the create_address_table migration and add columns. Also, set the foreign key for the order_id with the orders table:
<?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('addresses', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->string('name');
$table->string('phone');
$table->string('locality');
$table->text('address');
$table->string('city');
$table->string('state');
$table->string('country');
$table->string('landmark')->nullable();
$table->string('zip');
$table->string('type')->default('home');
$table->boolean('isdefault')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('addresses');
}
};
Define Columns for Transaction Table
In last, open the create_transaction_table migration and add the following columns. Set the foreign key for the User_id with the users table and order_id with the orders table:
<?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('transactions', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('order_id')->unsigned();
$table->enum('mode',['cod','card','paypal']);
$table->enum('status',['pending','approved','declined','refunded'])->default('pending');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('transactions');
}
};
Step 3: Migrate the Migrations
Now, let's migrate these migrations. In the command prompt, run the command:
php artisan migrate
All right, migration migrated!
Step 4: Define Relationships
Define Relationship in Order Model
Open the Order Model and add a function to relate the user. Create a function here:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
public function orderItems()
{
return $this->hasMany(OrderItem::class);
}
public function transaction()
{
return $this->hasOne(Transaction::class);
}
}
Now, for relating the order items, create a function orderItems and set the hasMany relationship. Create a function for shipping and set the hasOne relationship. In last, create a function for transaction and set hasOne relationship.
Define Relationship in OrderItem Model
Now, open the OrderItem Model and create a function for order and belongs to Order:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class OrderItem extends Model
{
use HasFactory;
public function product()
{
return $this->belongsTo(Product::class);
}
public function order()
{
return $this->belongsTo(Order::class);
}
public function review()
{
return $this->hasOne(Review::class,'order_item_id');
}
}
Define Relationship in Transaction Model
Now, go to the Transaction Model and copy this function and paste here, and also paste inside the transaction model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
use HasFactory;
public function order()
{
return $this->belongsTo(Order::class);
}
}
Now, everything is done related to the model migration and relationship for the checkout. We have created all four models and migrations, and set the relationships with models.
In this way, you can create a model and migration for checkout. In the next video, we will see the processing of checkout.
That's all about creating a model and migration for checkout.