📘 Module 3: Database Setup (MySQL)
1. Create MySQL Database pos_db
අපගේ POS පද්ධතියේ සියලුම දත්ත (products, sales, users) ගබඩා කර තැබීමට database එකක් අවශ්යයි. අපි මේ සඳහා MySQL භාවිතා කරමු.
XAMPP, WAMP, or MAMP වැනි local server environment එකක ඇති phpMyAdmin වැනි මෙවලමක් භාවිතා කර pos_db නමින් නව database එකක් සාදන්න.
Alternatively, you can use the SQL command:
CREATE DATABASE pos_db;
2. Configure .env File for Database Connection
දැන්, අපගේ Laravel project එකට database එක connect කරන්නේ කෙසේදැයි කිව යුතුය. මේ සඳහා project එකේ root directory එකේ ඇති .env file එක open කරගන්න.
එහි ඇති DB_* variables පහත පරිදි update කරන්න. බොහෝ විට local server එකක default username එක root වන අතර password එක හිස්ව තැබේ.
File: .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pos_db
DB_USERNAME=root
DB_PASSWORD=
3. Create Tables using Migrations
Migrations යනු database එකේ tables PHP code භාවිතයෙන් නිර්මාණය කිරීමට සහ කළමනාකරණය කිරීමට Laravel සපයන ක්රමයකි.
Terminal එක open කර products සහ sales සඳහා migrations නිර්මාණය කිරීමට පහත commands run කරන්න.
php artisan make:migration create_products_table
php artisan make:migration create_sales_table
මෙමගින් database/migrations/ ෆෝල්ඩරය තුල නව files දෙකක් නිර්මාණය වනු ඇත. දැන් අපි එම files වලට අවශ්ය columns එකතු කරමු.
File: database/migrations/..._create_products_table.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->decimal('price', 8, 2);
$table->integer('quantity');
$table->timestamps(); // created_at and updated_at
});
}
// ...
};
File: database/migrations/..._create_sales_table.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('sales', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained();
$table->integer('quantity');
$table->decimal('total_price', 10, 2);
$table->timestamps();
});
}
// ...
};
Migration files සකස් කළ පසු, පහත command එක run කිරීමෙන් database එකේ tables නිර්මාණය වේ.
php artisan migrate
දැන් phpMyAdmin වෙත ගොස් බැලුවහොත්, pos_db database එක තුල users, products, sales, සහ තවත් migrations-related tables කිහිපයක් සෑදී ඇති බව පෙනෙනු ඇත.
4. Sinhala Explanation: What is Migration & Schema
Migration (සංක්රමණය) සහ Schema (ව්යුහය) යනු කුමක්ද?
සරලවම කිවහොත්, Migration කියන්නේ අපේ database එකේ සිදුවන වෙනස්කම් (නව table එකක් සෑදීම, column එකක් එකතු කිරීම) පියවරෙන් පියවර, පිළිවෙලකට වාර්තා කර තබාගන්නා ක්රමයක්. හරියට අපි පොතක පරිච්ඡේද ලියනවා වගේ.
Schema (ස්කීමා) කියන්නේ ඒ database table එකේ සැලැස්ම (blueprint). ඒ කියන්නේ table එකේ නම, එහි තිබිය යුතු තීරු (columns) මොනවාද, එම තීරුවල ගබඩා කරන්නේ කුමන වර්ගයේ දත්ත ද (e.g., text, number, date) යන්නයි.
උදාහරණයක්:
අපි ගෙයක් හදන්න කලින් ඉංජිනේරුවෙක් ලවා plan එකක් (blueprint) අඳිනවා. ඒකේ කාමර ගාණ, දොරවල් තියෙන තැන් ඔක්කොම තියෙනවා. Schema එක කියන්නේ අන්න ඒ වගේ database table එකේ plan එක. Migration file එක කියන්නේ ඒ plan එක ලියලා තියෙන file එක. php artisan migrate command එක run කලාම, Laravel ඒ plan එක බලලා database එකේ table එක හදනවා.
මේ ක්රමය නිසා project එකේ වැඩ කරන කීප දෙනෙක්ට වුනත් database එකේ structure එක පහසුවෙන් share කරගන්න සහ update කරගන්න පුළුවන්.