Laravel 10 E-Commerce Project - Creating Model, Migration, and Relationship for Checkout

In this tutorial, we will learn how to create a model, migration, and relationship for checkout.

Let's explore the process of creating a model, migration, and relationship for checkout.

Creating Tables for Checkout

For checkout, we need four tables: Order, OrderItem, Shipping, and Transaction. Let's create these tables' models and migrations one by one.

Creating Model and Migration for Order Table

In the command prompt, create the model and migration for the order table:


Run the command

php artisan make:model Order –m

Creating Model and Migration for OrderItem Table

Create the model and migration for the OrderItem table:


php artisan make:model OrderItem –m

Creating Model and Migration for Shipping Table

Create the model and migration for the Shipping table:


php artisan make:model Shipping –m

Creating Model and Migration for Transaction Table

In the last step, create the model and migration for the Transaction table:


php artisan make:model Transaction –m

Defining Columns for Each Table

All four models and migrations have been created. Now, let's switch to the project.

Defining Columns for Orders Table

Open the create_orders_table migration and add the following columns:


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');
});
}

Defining Columns for OrderItem Table

Open the create_order_item_migration and add the following columns:


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');
});
}

Defining Columns for Shipping Table

Open the create_shipping_table migration and add the following columns:


public function up(): void
{
Schema::create('shippings', function (Blueprint $table) {
$table->id();
$table->bigInteger('order_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->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
});
}

Defining Columns for Transaction Table

In the last step, open the create_transaction_table migration and add the following columns:


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');
});
}

Migrating the Tables

Now, let's migrate these migrations. In the command prompt, run the command:


php artisan migrate

All right, the migration has been migrated.

Defining Relationships between Models

Open the Order Model and add functions to relate the user, order items, shipping, and transaction:


class Order extends Model
{
use HasFactory;

public function user()
{
return $this->belongsTo(User::class);
}

public function orderItems()
{
return $this->hasMany(OrderItem::class);
}

public function shipping()
{
return $this->hasOne(Shipping::class);
}

public function transaction()
{
return $this->hasOne(Transaction::class);
}
}

Defining Relationship for OrderItem Model

Open the OrderItem Model and create a function for order and belongs to Order:


class OrderItem extends Model
{
use HasFactory;

public function order()
{
return $this->belongsTo(Order::class);
}
}

Defining Relationship for Shipping Model

Go to the Shipping Model and add the following function:


class Shipping extends Model
{
use HasFactory;

public function order()
{
return $this->belongsTo(Order::class);
}
}

Defining Relationship for Transaction Model

Go to the Transaction Model and add the following function:


class Transaction extends Model
{
use HasFactory;

public function order()
{
return $this->belongsTo(Order::class);
}
}

Conclusion

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. By following these steps, you can create a model and migration for checkout.