1. Buat Controller Baru
Buka terminal Git Bash di VS Code, lalu jalankan perintah berikut untuk membuat LatihanController:
php artisan make:controller LatihanController
2. Buat File View untuk Tabel dan Form
Buat dua file Blade baru di dalam direktori resources/views/:
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Data Mahasiswa</title>
</head>
<body>
<h3>Data Mahasiswa</h3>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>No</th>
<th>NIM</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>NIM 1</td>
<td>Nama Lengkap 1</td>
<td>Kelas 1</td>
</tr>
<tr>
<td>2</td>
<td>NIM 2</td>
<td>Nama Lengkap 2</td>
<td>Kelas 2</td>
</tr>
</tbody>
</table>
</body>
</html>
- resources/views/form.blade.php
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Form Mahasiswa</title>
</head>
<body>
<form action="#" method="POST">
@csrf
<label for="nim">NIM</label><br>
<input type="text" id="nim" name="nim"><br><br>
<label for="nama">Nama Lengkap</label><br>
<input type="text" id="nama" name="nama"><br><br>
<label for="kelas">Kelas</label><br>
<input type="text" id="kelas" name="kelas"><br><br>
<button type="submit">Simpan</button>
</form>
</body>
</html>
3. Isi Method pada LatihanController.php
Buka file app/Http/Controllers/LatihanController.php dan buat dua function (getTabel dan getForm):
4. Daftarkan Route di routes/web.php
Buka file routes/web.php lalu tambahkan route berikut:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LatihanController; // Import Controller
Route::get('/tabel', [LatihanController::class, 'getTabel']);
Route::get('/form', [LatihanController::class, 'getForm']);
5. Pengujian di Browser
Pastikan server lokal berjalan (php artisan serve), lalu buka URL berikut di browser: