Tutorial Membuat Website Penerimaan Siswa Online Menggunakan Laravel 11
Berikut adalah langkah-langkah untuk membuat website penerimaan siswa online menggunakan Laravel 11.
1. Persiapan Lingkungan
- Instalasi Composer: Laravel memerlukan Composer untuk mengelola dependensinya.
- Instalasi Laravel 11:
composer create-project --prefer-dist laravel/laravel student-admission "11.*"
2. Konfigurasi Database
- Buka file
.env
dan sesuaikan konfigurasi database Anda:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=nama_database DB_USERNAME=username_database DB_PASSWORD=password_database
3. Membuat Model, Migration, dan Controller
- Buat model
Student
beserta migrasinya dan controller:php artisan make:model Student -mcr
- Edit file migration untuk tabel
students
:public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->string('phone'); $table->text('address'); $table->timestamps(); }); }
- Jalankan migrasi:
php artisan migrate
4. Membuat Route
- Edit file
routes/web.php
:use App\Http\Controllers\StudentController; Route::get('/students', [StudentController::class, 'index']); Route::get('/students/create', [StudentController::class, 'create']); Route::post('/students', [StudentController::class, 'store']); Route::get('/students/{student}', [StudentController::class, 'show']); Route::get('/students/{student}/edit', [StudentController::class, 'edit']); Route::put('/students/{student}', [StudentController::class, 'update']); Route::delete('/students/{student}', [StudentController::class, 'destroy']);
5. Implementasi Controller
- Edit file
app/Http/Controllers/StudentController.php
:namespace App\Http\Controllers; use App\Models\Student; use Illuminate\Http\Request; class StudentController extends Controller { public function index() { $students = Student::all(); return view('students.index', compact('students')); } public function create() { return view('students.create'); } public function store(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required|email|unique:students', 'phone' => 'required', 'address' => 'required', ]); Student::create($request->all()); return redirect()->route('students.index'); } public function show(Student $student) { return view('students.show', compact('student')); } public function edit(Student $student) { return view('students.edit', compact('student')); } public function update(Request $request, Student $student) { $request->validate([ 'name' => 'required', 'email' => 'required|email|unique:students,email,' . $student->id, 'phone' => 'required', 'address' => 'required', ]); $student->update($request->all()); return redirect()->route('students.index'); } public function destroy(Student $student) { $student->delete(); return redirect()->route('students.index'); } }
6. Membuat View
-
Buat folder
resources/views/students
. -
Buat file view untuk menampilkan, membuat, mengedit, dan menghapus siswa.
Index View (
resources/views/students/index.blade.php
):<h1>Daftar Siswa</h1> <a href="{{ route('students.create') }}">Tambah Siswa</a> <ul> @foreach ($students as $student) <li>{{ $student->name }}</li> @endforeach </ul>
Create View (
resources/views/students/create.blade.php
):<h1>Tambah Siswa</h1> <form action="{{ route('students.store') }}" method="POST"> @csrf <input type="text" name="name" placeholder="Nama"> <input type="email" name="email" placeholder="Email"> <input type="text" name="phone" placeholder="Telepon"> <textarea name="address" placeholder="Alamat"></textarea> <button type="submit">Simpan</button> </form>
Edit View (
resources/views/students/edit.blade.php
):<h1>Edit Siswa</h1> <form action="{{ route('students.update', $student->id) }}" method="POST"> @csrf @method('PUT') <input type="text" name="name" value="{{ $student->name }}"> <input type="email" name="email" value="{{ $student->email }}"> <input type="text" name="phone" value="{{ $student->phone }}"> <textarea name="address">{{ $student->address }}</textarea> <button type="submit">Update</button> </form>
Show View (
resources/views/students/show.blade.php
):<h1>Detail Siswa</h1> <p>Nama: {{ $student->name }}</p> <p>Email: {{ $student->email }}</p> <p>Telepon: {{ $student->phone }}</p> <p>Alamat: {{ $student->address }}</p>
7. Menjalankan Aplikasi
- Jalankan server Laravel:
php artisan serve
- Akses aplikasi di
http://localhost:8000/students
.
Sekarang Anda telah memiliki website penerimaan siswa online sederhana menggunakan Laravel 11.