පාරිභෝගික ලියාපදිංචි කිරීමේ ක්රියාවලිය, අප 5 වන මොඩියුලයේදී නිර්මාණය කළ රියදුරු ලියාපදිංචි කිරීමේ ක්රියාවලියට බොහෝ සෙයින් සමාන වේ.
අනුගමනය කළ යුතු පියවර:
- Routes: `routes/web.php` ගොනුවේ, customer registration form එක පෙන්වීමට (GET) සහ දත්ත ලබාගැනීමට (POST) routes දෙකක් `CustomerController` එකට යොමු කරන්න.
- Controller: `CustomerController` එක තුළ `create()` සහ `store()` නමින් functions දෙකක් සාදන්න.
- View: `resources/views/customers/create.blade.php` නමින් view file එකක් සාදා, එහි නම (`name`) සහ දුරකථන අංකය (`phone`) ලබාගැනීමට form එකක් නිර්මාණය කරන්න.
- Logic: `store()` function එක තුළ, `Request` එකෙන් එන දත්ත validate කර, `Customer` model එක භාවිතා කර database එකට save කරන්න.
දැන් අපි පද්ධතියේ වැදගත්ම කොටස වන Booking ක්රියාවලිය නිර්මාණය කරමු. මෙහිදී, අපට ලියාපදිංචි වී ඇති පාරිභෝගිකයින් සහ රියදුරන් dropdown මෙනු වලින් තෝරා ගැනීමට හැකි form එකක් අවශ්ය වේ.
1. Controller Logic: දත්ත View එකට ලබා දීම
`BookingController` එකේ `create` function එක තුළ, අපට සියලුම Customers සහ Drivers ලා database එකෙන් ලබාගෙන, ඒවා view එකට යැවිය යුතුය.
// app/Http/Controllers/BookingController.php
use App\Models\Customer;
use App\Models\Driver;
public function create()
{
$customers = Customer::all();
$drivers = Driver::all();
return view('bookings.create', [
'customers' => $customers,
'drivers' => $drivers
]);
}
2. View File (Blade Form) එක නිර්මාණය කිරීම
`resources/views/bookings/create.blade.php` file එක සාදා, පහත කේතය ඇතුළත් කරන්න. Controller එකෙන් එවන ලද `$customers` සහ `$drivers` variables, `foreach` loop එකක් මගින් dropdown options සෑදීමට යොදාගනී.
<!-- Dropdown for Customers -->
<div class="mb-3">
<label for="customer_id" class="form-label">Select Customer</label>
<select class="form-select" name="customer_id" required>
@foreach ($customers as $customer)
<option value="{{ $customer->id }}">{{ $customer->name }}</option>
@endforeach
</select>
</div>
<!-- Dropdown for Drivers -->
<div class="mb-3">
<label for="driver_id" class="form-label">Select Driver</label>
<select class="form-select" name="driver_id" required>
@foreach ($drivers as $driver)
<option value="{{ $driver->id }}">{{ $driver->name }} - {{ $driver->vehicle_type }}</option>
@endforeach
</select>
</div>
අවසාන වශයෙන්, booking form එක submit කළ විට, දත්ත validate කර, `bookings` table එකට save කර, සියලුම bookings ලැයිස්තුවක් ලෙස පෙන්වමු.
1. Booking දත්ත Save කිරීම (`store` function)
`BookingController` එකේ `store` function එක සාදන්න. මෙහිදී `customer_id` සහ `driver_id` වලංගු දැයි (`exists` rule) පරීක්ෂා කිරීම වැදගත් වේ.
// app/Http/Controllers/BookingController.php
use App\Models\Booking;
use Illuminate\Http\Request;
public function store(Request $request)
{
$validatedData = $request->validate([
'customer_id' => 'required|exists:customers,id',
'driver_id' => 'required|exists:drivers,id',
'booking_date' => 'required|date',
]);
Booking::create($validatedData);
return redirect('/bookings')->with('success', 'Booking created successfully!');
}
2. Bookings ලැයිස්තුව පෙන්වීම
සියලුම bookings, ඒවාට අදාළ customer සහ driver ගේ නම් සමඟින් පෙන්වීමට, අප Module 3 හිදී සෑදූ Eloquent Relationships භාවිතා කරමු. `BookingController` එකේ `index` function එක සාදන්න.
// In BookingController
public function index()
{
$bookings = Booking::with(['customer', 'driver'])->get();
return view('bookings.index', ['bookings' => $bookings]);
}
// In bookings/index.blade.php
@foreach ($bookings as $booking)
<tr>
<td>{{ $booking->id }}</td>
<td>{{ $booking->customer->name }}</td> <!-- Relationship Magic! -->
<td>{{ $booking->driver->name }}</td> <!-- Relationship Magic! -->
<td>{{ $booking->booking_date }}</td>
<td>{{ $booking->status }}</td>
</tr>
@endforeach
$booking->customer->name ලෙස ඉතා පහසුවෙන් සම්බන්ධිත දත්ත ලබා ගැනීමට Eloquent Relationships අපට උදව් වේ. දැන් අපගේ පද්ධතියේ මූලික ක්රියාකාරීත්වය සම්පූර්ණයි!