In this tutorial we are going to learn about Create Model, Migration and Relationship for Checkout.
So let see how can we Create Model ,Migration and Relationship for Checkout.
For Checkout we need four tables
First table is for Order, Second table is for OrderItem, third table is for Shipping and fourth table is for Transaction
So lets create these table’s model and migration one by one
In command prompt for creating the model and migration for order table
Now create the model and migration for OrderItem
Now create the model and migration for Shipping
In last create model and migration for Transaction
Alright all four model and migration have been created
Now switch to the project
Lets open the create_orders_table migration
And inside this migration lets add these columns
Now lets open create_order_item_migration
And add inside this migration add these columns
Now lets open the create_shipping_table migration and these columns
In last open create_transaction_table migration and add the following columns
Alright now lets migrate these migration
So in command prompt run the command
All right migration migrated
Now open Order Model
And add function for relate the user,orderitems,shipping and transaction
So create a functions here
Now open OrderItem Model
And also create a funcition or order and belongs to Order
Now go to the Shipping Model and add the following funcition
Now go to the Transaction Model and the following funcition
Now everything is done related to the model migration and relationship for the checkout
We have created all four model and migrations
And set the relationship with models
So in this way you can Create Model and Migration for Checkout
So let see how can we Create Model ,Migration and Relationship for Checkout.
For Checkout we need four tables
First table is for Order, Second table is for OrderItem, third table is for Shipping and fourth table is for Transaction
So lets create these table’s model and migration one by one
In command prompt for creating the model and migration for order table
Run the command
php artisan make:model Order –m
Now create the model and migration for OrderItem
php artisan make:model OrderItem –m
Now create the model and migration for Shipping
php artisan make:model Shipping –m
In last create model and migration for Transaction
php artisan make:model Transaction –m
Alright all four model and migration have been created
Now switch to the project
Lets open the create_orders_table migration
And inside this migration lets add these 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');
});
}
Now lets open create_order_item_migration
And add inside this migration add these 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');
});
}
Now lets open the create_shipping_table migration and these 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');
});
}
In last open 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');
});
}
Alright now lets migrate these migration
So in command prompt run the command
php artisan migrate
All right migration migrated
Now open Order Model
And add function for relate the user,orderitems,shipping and transaction
So create a functions here
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);
}
}
Now open OrderItem Model
And also create a funcition or order and belongs to Order
class OrderItem extends Model
{
use HasFactory;
public function order()
{
return $this->belongsTo(Order::class);
}
}
Now go to the Shipping Model and add the following funcition
class Shipping extends Model
{
use HasFactory;
public function order()
{
return $this->belongsTo(Order::class);
}
}
Now go to the Transaction Model and the following funcition
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 model and migrations
And set the relationship with models
So in this way you can Create Model and Migration for Checkout