ඒකකය 6: Read (දත්ත පෙන්වීම)

අපි දැන් දත්ත ඇතුළත් කර අවසන්. ඊළඟට, CRUD හි "Read" කොටස ක්‍රියාත්මක කරමු. මෙහිදී, `students` වගුවේ ඇති සියලුම දත්ත ලබාගෙන ඒවා පරිශීලකයාට පෙනෙන සේ වගුවක (table) ප්‍රදර්ශනය කරමු.

1. Controller හි දත්ත ලබාගැනීමේ ක්‍රමය (Fetch Data)

පළමුව, `app/Http/Controllers/StudentController.php` ගොනුව විවෘත කර, `index` නමින් නව ශ්‍රිතයක් (method) එක් කරන්න. අපි 4 වන ඒකකයේදී මෙම method එක සඳහා route එකක් දැනටමත් සාදා ඇත. මෙම method එක මගින් `Student` model එක භාවිතා කර `students` table එකේ ඇති සියලුම දත්ත ලබාගෙන, ඒවා `index` නම් view එකකට යවයි.


namespace App\Http\Controllers;

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

class StudentController extends Controller
{
    public function index()
    {
        // Student model එක හරහා students table එකේ සියලුම දත්ත ලබාගැනීම
        $students = Student::all(); 

        // දත්ත 'students' නම් විචල්‍යයක් ලෙස view එකට යැවීම
        return view('index', compact('students'));
    }

    // ... create සහ store methods
}

`compact('students')` මගින් `$students` නම් PHP විචල්‍යය, view එක තුළදීද `students` යන නමින්ම භාවිතා කළ හැකි වන සේ යවයි.

2. දත්ත පෙන්වීම සඳහා Blade View එක සෑදීම

දැන් `resources/views/` ෆෝල්ඩරය තුළ `index.blade.php` නමින් නව ගොනුවක් සාදන්න. Controller එකෙන් එවන `students` දත්ත එකතුව (collection) `foreach` loop එකක් භාවිතයෙන් දිවගොස් (iterate), එක් එක් ශිෂ්‍යයාගේ තොරතුරු වගුවක පේළියක (row) පෙන්වමු.

<!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>
        <a href="/create" class="btn btn-primary mb-3">නව ශිෂ්‍යයෙකු එක් කරන්න</a>
        
        @if (session('status'))
            <div class="alert alert-success">
                {{ session('status') }}
            </div>
        @endif

        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>නම</th>
                    <th>ඊමේල්</th>
                    <th>දුරකථන අංකය</th>
                    <th>පාඨමාලාව</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($students as $student)
                <tr>
                    <td>{{ $student->id }}</td>
                    <td>{{ $student->name }}</td>
                    <td>{{ $student->email }}</td>
                    <td>{{ $student->phone }}</td>
                    <td>{{ $student->course }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</body>
</html>

ඉහත කේතයේ, @if (session('status')) කොටස මගින්, පෙර පාඩමේදී දත්ත ඇතුළත් කළ පසු අප යොමුකළ (redirect) සාර්ථක පණිවිඩය පෙන්වයි.

3. Route එක තහවුරු කිරීම

`routes/web.php` ගොනුවේ මෙම route එක ඇති බවට වග බලා ගන්න. අපි මෙය 4 වන ඒකකයේදී එක් කළෙමු.

Route::get('/students', [StudentController::class, 'index']);

දැන් සියල්ල සූදානම්! ✅ `php artisan serve` ක්‍රියාත්මක කර, `http://127.0.0.1:8000/students` වෙත පිවිසෙන්න. ඔබ දත්ත සමුදායට ඇතුළත් කළ සියලුම ශිෂ්‍ය දත්ත පිළිවෙලට සකසන ලද වගුවක දිස්වනු ඇත. ඊළඟට, මෙම දත්ත යාවත්කාලීන කරන (Update) ආකාරය බලමු.