Laravel 12 E-Commerce Project Tutorial

Creating Models, Migrations, and Relationships for Checkout

Hi everyone, welcome back to the Laravel E-Commerce Tutorial. This video focuses on setting up the necessary database structure for the Checkout process, including models, migrations, and relationships.

For the checkout functionality, we need four tables: Order, OrderItem, Address, and Transaction.

  1. Create Models and Migrations

    Run the following commands in the command prompt to create the models and their respective migrations:

    php artisan make:model Order –m
    php artisan make:model OrderItem –m
    php artisan make:model Address –m
    php artisan make:model Transaction –m
  2. Define the `orders` Table Schema

    Update the `create_orders_table` migration file with the following columns and foreign key constraint:

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

  3. Define the `addresses` Table Schema

    Update the `create_addresses_table` migration file with the following columns:

    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();
    });
    }
  4. Define the `transactions` Table Schema

    Update the `create_transactions_table` migration file with the transaction details and foreign keys for `user_id` and `order_id`:

    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");
    });
    }
  5. Run the Migrations

    Execute the migrations to create the new tables in the database:

    php artisan migrate
  6. Define Model Relationships

    Finally, define the Eloquent relationships in the respective models:

    `Order` Model (`Order.php`)

    The `Order` model has a relationship with a single user, many order items, and a single transaction:

    namespace AppModels;
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    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);
    }
    }

    `OrderItem` Model (`OrderItem.php`)

    The `OrderItem` model belongs to an order and a product:

    namespace AppModels;
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    class OrderItem extends Model
    {
    use HasFactory;
    public function product()
    {
    return $this->belongsTo(Product::class);
    }
    public function order()
    {
    return $this->belongsTo(Order::class);
    }
    }

    `Transaction` Model (`Transaction.php`)

    The `Transaction` model belongs to an order:

    namespace AppModels;
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    class Transaction extends Model
    {
    use HasFactory;
    public function order()
    {
    return $this->belongsTo(Order::class);
    }
    }

Conclusion ✅

All four models and migrations for the checkout process have been created, and the necessary relationships between them have been established.