E-commerce Guide: PHP, Laravel and MySQL

E-commerce Website Guide

1. Project idea

Build a Cambodian online shop for fashion, beauty products, electronics, groceries, or local crafts. Start with a small catalog and support English, Khmer, Cambodian riel, and cash on delivery.

2. Recommended stack

  • Backend: PHP with Laravel

  • Frontend: Laravel Blade with Tailwind CSS, or React later

  • Database: MySQL

  • Authentication: Laravel starter kit

  • Version control: Git and GitHub

3. First version features

  • Register and log in

  • Browse products

  • Search and filter products

  • View product details

  • Add and remove cart items

  • Create an order

  • View order history

  • Admin product management

  • Cash-on-delivery checkout

4. Create the project

composer create-project laravel/laravel cambodia-shop
cd cambodia-shop
php artisan key:generate
php artisan migrate
php artisan serve

Set the database values in .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cambodia_shop
DB_USERNAME=root
DB_PASSWORD=

For authentication, install a Laravel starter kit suitable for your Laravel version. Follow the official Laravel authentication documentation and run its migrations.

5. Suggested folder structure

cambodia-shop/
├── app/
│   ├── Http/Controllers/
│   └── Models/
├── database/
│   ├── migrations/
│   └── seeders/
├── resources/views/
│   ├── layouts/
│   ├── products/
│   ├── cart/
│   ├── orders/
│   └── admin/
├── routes/web.php
├── public/
└── README.md

6. Database tables

users

  • id

  • name

  • email

  • password

  • role

products

  • id

  • name

  • description

  • price

  • image

  • category

  • stock

  • timestamps

orders

  • id

  • user_id

  • total

  • delivery_address

  • payment_method

  • status

  • timestamps

order_items

  • id

  • order_id

  • product_id

  • quantity

  • price

  • timestamps

7. Create models and migrations

php artisan make:model Product -mcr
php artisan make:model Order -mcr
php artisan make:model OrderItem -m
php artisan make:seeder ProductSeeder

Example products migration:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->text('description');
    $table->decimal('price', 10, 2);
    $table->string('image')->nullable();
    $table->string('category');
    $table->unsignedInteger('stock')->default(0);
    $table->timestamps();
});

Run migrations:

php artisan migrate

8. Product model

// app/Models/Product.php
class Product extends Model
{
    protected $fillable = [
        'name', 'description', 'price', 'image', 'category', 'stock'
    ];
}

9. Routes

use App\Http\Controllers\ProductController;
use App\Http\Controllers\OrderController;

Route::get('/', [ProductController::class, 'index']);
Route::resource('products', ProductController::class)->only([
    'index', 'show'
]);

Route::middleware('auth')->group(function () {
    Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
    Route::post('/cart/{product}', [CartController::class, 'store'])->name('cart.store');
    Route::post('/orders', [OrderController::class, 'store'])->name('orders.store');
    Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
});

10. Controller example

public function index(Request $request)
{
    $products = Product::query()
        ->when($request->search, function ($query, $search) {
            $query->where('name', 'like', "%{$search}%");
        })
        ->latest()
        ->paginate(12);

    return view('products.index', compact('products'));
}

11. Blade product card

@foreach ($products as $product)
    
{{ $product->name }}

{{ $product->name }}

{{ number_format($product->price) }} KHR

View product
@endforeach {{ $products->links() }}

12. Development order

  1. Configure MySQL and authentication.

  1. Create migrations, models, and seed data.

  1. Build the product listing and details pages.

  1. Add search, categories, and pagination.

  1. Build a session-based cart.

  1. Create checkout and order tables.

  1. Add order history.

  1. Add admin product and order management.

  1. Add Khmer and English labels and Cambodian delivery fields.

  1. Test and deploy.

13. Important security and quality checks

  • Use Laravel validation for every form.

  • Protect forms with CSRF tokens.

  • Use authorization policies for admin actions.

  • Hash passwords through Laravel authentication.

  • Recalculate order totals on the server.

  • Check stock before saving an order.

  • Keep secrets in .env.

  • Use eager loading where appropriate.

14. Portfolio improvements

  • Khmer and English language switcher

  • Prices in KHR

  • Province and district delivery fields

  • Telegram order notifications

  • Product reviews

  • Order status tracking

  • Feature and unit tests

  • Live deployment

15. Testing checklist

  • A user can register and log in.

  • Invalid input is rejected.

  • Products and images display correctly.

  • Search and pagination work.

  • Cart quantities update correctly.

  • Empty carts cannot be checked out.

  • Stock is checked during checkout.

  • Customers cannot access admin pages.

  • Orders appear in the correct account.

  • The site works on mobile.

16. README outline

# Cambodia Shop

A Laravel and MySQL e-commerce application for Cambodian customers.

## Features
## Technologies
## Screenshots
## Installation
## Environment variables
## Database setup
## Demo account
## Future improvements

Build the first version with fake products and cash on delivery. Add online payments only after the basic ordering flow works.