ඕනෑම යෙදුමක් (application) නිර්මාණය කිරීමට පෙර එහි දත්ත සමුදා ව්යුහය (Database Schema) සැලසුම් කිරීම ඉතා වැදගත් වේ. අපගේ දත්ත ගබඩා වන ආකාරය සහ tables අතර සබඳතා මින් පැහැදිලි වේ.
අපගේ booking system එක සඳහා ප්රධාන tables 3ක් අවශ්ය වේ:
-
1. `drivers` - රියදුරන්ගේ තොරතුරු ගබඩා කිරීමට.
Columns: id, name, phone, national_id, vehicle_type, vehicle_id -
2. `customers` - පාරිභෝගිකයින්ගේ තොරතුරු ගබඩා කිරීමට.
Columns: id, name, phone -
3. `bookings` - සිදුකරන වෙන්කිරීම් (orders) පිළිබඳ තොරතුරු ගබඩා කිරීමට. මෙම table එක `drivers` සහ `customers` tables දෙකටම සම්බන්ධ වේ.
Columns: id, customer_id, driver_id, booking_date, status
Laravel Migrations යනු Git වැනි version control පද්ධතියක් භාවිතයෙන් අපගේ දත්ත සමුදායේ ව්යුහය කළමනාකරණය කිරීමට ඇති ක්රමයකි. PHP කේත භාවිතයෙන් අපට database tables නිර්මාණය කිරීමට, වෙනස් කිරීමට සහ ඉවත් කිරීමට හැකියාව ලැබේ.
දැන්, අපගේ terminal එකේ `php artisan make:migration` විධානය භාවිතයෙන් ඉහත tables තුන සඳහා migration files නිර්මාණය කරමු.
php artisan make:migration create_drivers_table
php artisan make:migration create_customers_table
php artisan make:migration create_bookings_table
දැන් අපි migration files edit කර අපගේ tables වලට අවශ්ය columns (තීරු) මොනවාදැයි define කරමු. `create_drivers_table` migration file එක open කර එහි `up` function එක පහත පරිදි සකස් කරන්න:
public function up(): void
{
Schema::create('drivers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('phone')->unique();
$table->string('national_id')->unique();
$table->string('vehicle_type'); // e.g., 'Car', 'Van', 'Tuk'
$table->string('vehicle_id')->unique(); // License plate
$table->timestamps();
});
}
`bookings` table එකේදී, `customer_id` සහ `driver_id` සඳහා foreign key සබඳතා ඇති කිරීමට `constrained()` method එක භාවිතා කරන්න:
public function up(): void
{
Schema::create('bookings', function (Blueprint $table) {
$table->id();
$table->foreignId('customer_id')->constrained()->onDelete('cascade');
$table->foreignId('driver_id')->constrained()->onDelete('cascade');
$table->dateTime('booking_date');
$table->string('status')->default('pending'); // e.g., pending, confirmed, completed
$table->timestamps();
});
}
අපගේ migration files වල tables වල ව්යුහය define කර අවසන්. දැන් එක විධානයකින් Laravel මගින් database එකේ මෙම tables සියල්ල නිර්මාණය කරනු ඇත.
ඔබගේ terminal එකේ පහත Artisan විධානය ක්රියාත්මක කරන්න:
php artisan migrate