ඒකකය 5: Create (දත්ත ඇතුළත් කිරීම)

දැන් අපගේ CRUD යෙදුමේ පළමු ක්‍රියාවලිය වන "Create" කොටස සකස් කරමු. මෙහිදී අපි නව ශිෂ්‍යයෙකුගේ තොරතුරු ඇතුළත් කිරීමට පෝරමයක් (form) නිර්මාණය කර, එම දත්ත දත්ත සමුදායේ ගබඩා කරමු.

1. දත්ත ඇතුළත් කිරීමේ පෝරමය (Blade Template)

පළමුව, `resources/views/` ෆෝල්ඩරය තුළ `create.blade.php` නමින් නව ගොනුවක් සාදන්න. මෙම ගොනුවට Bootstrap භාවිතයෙන් සකසන ලද පහත HTML කේතය ඇතුළත් කරන්න.

<!DOCTYPE html>
<html lang="si">
<head>
    <meta charset="UTF-8">
    <title>නව ශිෂ්‍යයෙකු ඇතුළත් කරන්න</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h2>නව ශිෂ්‍යයෙකු ඇතුළත් කරන්න</h2>
        <form action="/store" method="POST">
            @csrf
            <div class="mb-3">
                <label for="name" class="form-label">නම</label>
                <input type="text" class="form-control" id="name" name="name">
            </div>
            <div class="mb-3">
                <label for="email" class="form-label">ඊමේල් ලිපිනය</label>
                <input type="email" class="form-control" id="email" name="email">
            </div>
            <div class="mb-3">
                <label for="phone" class="form-label">දුරකථන අංකය</label>
                <input type="text" class="form-control" id="phone" name="phone">
            </div>
            <div class="mb-3">
                <label for="course" class="form-label">පාඨමාලාව</label>
                <input type="text" class="form-control" id="course" name="course">
            </div>
            <button type="submit" class="btn btn-primary">ඇතුළත් කරන්න</button>
        </form>
    </div>
</body>
</html>

සටහන: @csrf යනු Laravel හි ඇති Blade directive එකකි. මෙය Cross-Site Request Forgery (CSRF) ප්‍රහාර වලින් ඔබගේ යෙදුම ආරක්ෂා කරයි. සෑම POST, PUT, DELETE, PATCH පෝරමයකම මෙය අනිවාර්යයෙන්ම ඇතුළත් කළ යුතුය.

2. Controller හි දත්ත ගබඩා කිරීමේ ක්‍රමය (Store Method)

දැන්, `app/Http/Controllers/StudentController.php` ගොනුව විවෘත කර, පෝරමයෙන් එන දත්ත භාරගෙන දත්ත සමුදායේ ගබඩා කිරීම සඳහා `store` නමින් නව ශ්‍රිතයක් (method) එක් කරන්න.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student; // Student Model එක import කරන්න

class StudentController extends Controller
{
    // ... වෙනත් methods (තිබේ නම්)

    public function store(Request $request)
    {
        // 1. නව Student object එකක් සාදන්න
        $student = new Student;

        // 2. පෝරමයෙන් එන දත්ත student object එකට පවරන්න
        $student->name = $request->name;
        $student->email = $request->email;
        $student->phone = $request->phone;
        $student->course = $request->course;

        // 3. දත්ත සමුදායේ ගබඩා කරන්න
        $student->save();

        // 4. සාර්ථක පණිවිඩයක් සමඟින් redirect කරන්න
        return redirect('/students')->with('status', 'ශිෂ්‍ය දත්ත සාර්ථකව ඇතුළත් කරන ලදී!');
    }
}

3. Routes සැකසීම

අවසාන වශයෙන්, `routes/web.php` ගොනුවට ගොස් පෝරමය පෙන්වීම සඳහා (`create` method) සහ දත්ත ගබඩා කිරීම සඳහා (`store` method) අවශ්‍ය routes දෙක එක් කරන්න.


use App\Http\Controllers\StudentController;

// ... වෙනත් routes

// පෝරමය පෙන්වීම සඳහා route
Route::get('/create', [StudentController::class, 'create']);

// පෝරමයේ දත්ත ගබඩා කිරීම සඳහා route
Route::post('/store', [StudentController::class, 'store']);

මීට අමතරව, `StudentController` එකට `create` method එකද එක් කළ යුතුය. එමගින් අප සෑදූ `create.blade.php` view එක පෙන්වයි.


// StudentController.php ගොනුව තුළ
public function create()
{
    return view('create');
}

දැන් සියල්ල සූදානම්! 🚀 ඔබේ development server එක (`php artisan serve`) ක්‍රියාත්මක කර, බ්‍රව්සරයෙන් `http://127.0.0.1:8000/create` වෙත පිවිසෙන්න. පෝරමය පුරවා submit කළ විට, දත්ත දත්ත සමුදායේ `students` වගුවේ ගබඩා වනු ඇත.