Kali ini kita akan belajar tentang membuat E-Commerce sederhana di Laravel. Dalam tutorial ini akan memandu pembaca melalui langkah demi langkah mulai dari dasar hingga fungsionalitas utama e-commerce, seperti katalog produk, keranjang belanja, dan integrasi pembayaran.

A. Buat Project Laravel Baru

Instalasi Laravel

Jika belum, install Laravel secara global melalui Composer:

composer global require laravel/installer

Buat Project Laravel

Mulailah dengan membuat project Laravel baru bernama ecommerce:

laravel new ecommerce

atau, jika menggunakan Composer secara langsung:

composer create-project --prefer-dist laravel/laravel ecommerce

Akses Project Setelah instalasi selesai, masuk ke direktori project dengan:

cd ecommerce

Jalankan Server Laravel Untuk memastikan project Laravel berjalan, jalankan perintah:

php artisan serve

Cek di browser dengan membuka http://localhost:8000 untuk melihat halaman welcome dari Laravel.


B. Konfigurasi Koneksi Database

Setting .env File

Buka file .env di root project, dan konfigurasi database agar terhubung dengan MySQL:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ecommerce
DB_USERNAME=root
DB_PASSWORD=your_password

Pastikan nilai DB_DATABASE sesuai dengan nama database yang akan kita buat, yaitu ecommerce. Sesuaikan DB_USERNAME dan DB_PASSWORD dengan kredensial MySQL di komputer kamu.

Buat Database MySQL

Buat database baru bernama ecommerce:

CREATE DATABASE ecommerce;

Gunakan tool seperti phpMyAdmin atau adminer untuk membuat database code di atas, atau bisa melalui MySQL CLI dalam hal ini saya menggunakan mySQL workbanch.

Migrate Database

Jalankan migrasi default Laravel untuk membuat tabel-tabel autentikasi:

php artisan migrate

Catatan: Jika migrasi berhasil, kamu akan melihat tabel users, password_resets, dan failed_jobs di database ecommerce.


C. Buat Tabel Utama untuk E-commerce

Sekarang kita akan membuat tabel tambahan yang diperlukan untuk modul e-commerce ini.

Buat Migration untuk tabel brands:

Jalankan perintah berikut untuk membuat migration baru:

php artisan make:migration create_brands_table

Tambahkan kolom di dalam migration:

Buka file create_brands_table.php di folder database/migrations, lalu tambahkan kolom-kolom yang dibutuhkan:

public function up()
{
    Schema::create('brands', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('slug')->unique();
        $table->string('logo')->nullable();
        $table->timestamps();
    });
}

Migration Product Table

Buat migration untuk tabel products:

php artisan make:migration create_products_table

Buka file migrasi baru di database/migrations/xxxx_xx_xx_xxxxxx_create_products_table.php dan ubah menjadi seperti berikut:

<?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('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->decimal('price', 8, 2);
            $table->string('sku')->unique();
            $table->string('image')->nullable();
            $table->integer('stock')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('products');
    }
};

Category Table

Buat migration untuk tabel categories:

php artisan make:migration create_categories_table

Buka file migrasi create_categories_table.php dan ubah menjadi:

<?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) {
            $table->id();
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('categories');
    }
};

Product_Category Pivot Table

Tabel pivot ini menghubungkan produk dengan kategori.

php artisan make:migration create_product_category_table --create=product_category

Ubah file create_product_category_table.php menjadi:

<?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('product_category', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->foreignId('category_id')->constrained()->onDelete('cascade');
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('product_category');
    }
};

Orders Table

Migration untuk tabel orders akan mencatat detail pesanan.

php artisan make:migration create_orders_table

Buka file create_orders_table.php dan ubah menjadi:

<?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('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->decimal('total', 10, 2);
            $table->string('status')->default('pending');
            $table->string('payment_status')->default('unpaid');
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('orders');
    }
};

Order_Items Table

Migration ini untuk detail setiap item dalam pesanan.

php artisan make:migration create_order_items_table

Buka file create_order_items_table.php dan ubah menjadi:

<?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('order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->integer('quantity');
            $table->decimal('price', 8, 2);
            $table->timestamps();
        });
    }

    public function down(): void {
        Schema::dropIfExists('order_items');
    }
};

Jalankan Semua Migrasi

Setelah semua file migrasi siap, jalankan migrasi:

php artisan migrate

Jika berhasil, semua tabel yang dibutuhkan akan terbentuk di database ecommerce. Kira kira seperti ini tabel-tabel nya