In this video 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
So switch to the command prompt and run the following command.


php artisan make:model Order –m

php artisan make:model OrderItem –m

php artisan make:model Shipping –m

php artisan make:model Transaction –m


Now lets open the create_orders_table migration and add the following code.



<?php

use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;

class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
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('firstname');
$table->string('lastname');
$table->string('mobile');
$table->string('email');
$table->string('line1');
$table->string('line2')->nullable();
$table->string('city');
$table->string('province');
$table->string('country');
$table->string('zipcode');
$table->enum('status',['ordered','delivered','canceled'])->default('ordered');
$table->boolean('is_shipping_different')->default(false);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}


Now lets open the create_order_items_table migration and add the following code.

<?php

use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;

class CreateOrderItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
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->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_items');
}
}

Now lets open the create_shippings_table migration and add the following code.

<?php

use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;

class CreateShippingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shippings', function (Blueprint $table) {
$table->id();
$table->bigInteger('order_id')->unsigned();
$table->string('firstname');
$table->string('lastname');
$table->string('mobile');
$table->string('email');
$table->string('line1');
$table->string('line2')->nullable();
$table->string('city');
$table->string('province');
$table->string('country');
$table->string('zipcode');
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shippings');
}
}

Now lets open the create_transactions_table migration and add the following code.


<?php

use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;

class CreateTransactionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
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.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('transactions');
}
}


Now open Order Model add the following code.

<?php

namespace App\\Models;

use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
use Illuminate\\Database\\Eloquent\\Model;

class Order extends Model
{
use HasFactory;

protected $table = \"orders\";

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 the OrderItem model and add the following code.

<?php

namespace App\\Models;

use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
use Illuminate\\Database\\Eloquent\\Model;

class OrderItem extends Model
{
use HasFactory;

protected $table = \"order_items\";

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

}


Now open the Shipping model and add the following code.

<?php

namespace App\\Models;

use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
use Illuminate\\Database\\Eloquent\\Model;

class Shipping extends Model
{
use HasFactory;

protected $table = \"shippings\";

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


Now open the Transaction model and add the following code.

<?php

namespace App\\Models;

use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
use Illuminate\\Database\\Eloquent\\Model;

class Transaction extends Model
{
use HasFactory;

protected $table = \"transactions\";

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



We have created all four model,migration and relationship with models.
So in this way you can Create Model, Migration and relationship for Checkout.