Project & Database Setup
Create a new Laravel project and set up the database schema for an e-commerce platform.
Task
In this stage, you'll create a new Laravel project and set up the database schema for an e-commerce platform. You'll need to create migrations for all the required tables.
Required Tables
- id
- name
- password
- address
- phone
- id
- name
- description
- price
- stock_quantity
- category_id
- id
- name
- description
- parent_id
- id
- user_id
- total_amount
- status
- shipping_address
- payment_method
- id
- order_id
- product_id
- quantity
- unit_price
- id
- product_id
- user_id
- rating
- comment
- id
- user_id
- product_id
Implementation Steps
1. Create a new Laravel project
composer create-project laravel/laravel ecommerce-workshop2. Configure your database
Update your .env file with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ecommerce_workshop
DB_USERNAME=root
DB_PASSWORD=3. Create migrations for each table
Run the following commands to create migration files:
php artisan make:migration create_categories_table
php artisan make:migration create_products_table
php artisan make:migration create_orders_table
php artisan make:migration create_order_items_table
php artisan make:migration create_reviews_table
php artisan make:migration create_wishlists_table4. Implement the migrations
Here's a template for the categories migration. You need to fill in the appropriate code:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ public function up(): void { Schema::create('categories', function (Blueprint $table) { // TODO: Add columns for the categories table // - id (auto-incremented primary key) // - name (string) // - description (nullable text) // - parent_id (nullable foreign key to categories table) // - timestamps // Your code here... }); } public function down(): void { // TODO: Drop the categories table // Your code here... }};Now implement the remaining migrations following the same pattern:
- Products migration: Include columns for id, name, description, price, stock_quantity, category_id (foreign key), and timestamps.
- Orders migration: Include columns for id, user_id (foreign key), total_amount, status, shipping_address, payment_method, and timestamps.
- Order_Items migration: Include columns for id, order_id (foreign key), product_id (foreign key), quantity, unit_price, and timestamps.
- Reviews migration: Include columns for id, product_id (foreign key), user_id (foreign key), rating, comment, and timestamps.
- Wishlists migration: Include columns for id, user_id (foreign key), product_id (foreign key), and timestamps.
Hint:
Remember to use the appropriate column types for each field. For example, use $table->string('name') for string columns, $table->text('description') for text columns, and$table->foreignId('category_id')->constrained() for foreign keys.
5. Run the migrations
php artisan migrateCheckpoint 1
Run the following command and share a screenshot of the output on Slack:
php artisan migrate:statusThe output should show all your migrations have run successfully.