CRUD Kelas

This commit is contained in:
Rizky 2025-04-24 18:33:58 +07:00
parent 44f26f1b5a
commit 0b96fd767e
6 changed files with 128 additions and 11 deletions

View File

@ -2,13 +2,53 @@
namespace App\Http\Controllers;
use App\Models\Jurusan;
use App\Models\Kelas;
use Illuminate\Http\Request;
class KelasController extends Controller
{
public function index(){
$kelas = Kelas::select('id', 'nama_kelas')->with(['jurusan:id,nama_jurusan'])->get();
return view('admin.kelas', compact('kelas'));
$kelas = Kelas::with('jurusan')->get();
return view('admin.kelas.index', compact('kelas'));
}
public function create(){
$jurusan = Jurusan::all();
return view('admin.kelas.create', compact('jurusan'));
}
public function store(Request $request){
$request->validate([
'nama_kelas' => 'required',
'id_jurusan' => 'required|exists:jurusan,id',
]);
Kelas::create($request->all());
return redirect()->route('admin.kelas.index')->with('success', 'Kelas berhasil ditambahkan');
}
public function edit(Kelas $kelas)
{
$jurusan = Jurusan::all();
return view('admin.kelas.edit', compact('kelas', 'jurusan'));
}
public function update(Request $request, Kelas $kelas){
$request->validate([
'nama_kelas' => 'required',
'id_jurusan' => 'required|exists:jurusan,id',
]);
$kelas->update($request->all());
return redirect()->route('admin.kelas.index')->with('success', 'Kelas berhasil diperbarui');
}
public function destroy(Kelas $kelas)
{
$kelas->delete();
return redirect()->route('admin.kelas.index')->with('success', 'Kelas berhasil dihapus');
}
}

View File

@ -43,8 +43,8 @@ class="block p-2 rounded-lg {{ request()->is('admin/siswa') ? 'bg-blue-100 text-
<li><a href="{{ route('admin.guru') }}"
class="block p-2 rounded-lg {{ request()->is('admin/guru') ? 'bg-blue-100 text-blue-600' : 'bg-gray-50 text-gray-800' }}">Guru</a>
</li>
<li><a href="{{ route('admin.kelas') }}"
class="block p-2 rounded-lg {{ request()->is('admin/kelas') ? 'bg-blue-100 text-blue-600' : 'bg-gray-50 text-gray-800' }}">Kelas</a>
<li><a href="{{ route('admin.kelas.index') }}"
class="block p-2 rounded-lg {{ request()->is('admin/kelas/index') ? 'bg-blue-100 text-blue-600' : 'bg-gray-50 text-gray-800' }}">Kelas</a>
</li>
<li><a href="{{ route('admin.jurusan.index') }}"
class="block p-2 rounded-lg {{ request()->is('admin/jurusan/index') ? 'bg-blue-100 text-blue-600' : 'bg-gray-50 text-gray-800' }}">Jurusan</a>

View File

@ -0,0 +1,32 @@
@extends('layouts.dashboard')
@section('title', 'Tambah Kelas')
@section('content')
<div class="bg-white shadow-md rounded-lg p-6 max-w-lg mx-auto">
<h1 class="text-2xl font-semibold mb-4">Tambah Kelas</h1>
<form action="{{ route('admin.kelas.store') }}" method="POST">
@csrf
<div class="mb-4">
<label class="block mb-1 font-medium">Nama Kelas</label>
<input type="text" name="nama_kelas" class="w-full border px-4 py-2 rounded" required>
</div>
<div class="mb-4">
<label class="block mb-1 font-medium">Jurusan</label>
<select name="id_jurusan" class="w-full border px-4 py-2 rounded" required>
<option value="">-- Pilih Jurusan --</option>
@foreach($jurusan as $item)
<option value="{{ $item->id }}">{{ $item->nama_jurusan }}</option>
@endforeach
</select>
</div>
<div class="flex justify-end">
<a href="{{ route('admin.kelas.index') }}" class="bg-gray-300 px-4 py-2 rounded mr-2">Batal</a>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">Simpan</button>
</div>
</form>
</div>
@endsection

View File

@ -0,0 +1,35 @@
@extends('layouts.dashboard')
@section('title', 'Edit Kelas')
@section('content')
<div class="bg-white shadow-md rounded-lg p-6 max-w-lg mx-auto">
<h1 class="text-2xl font-semibold mb-4">Edit Kelas</h1>
<form action="{{ route('admin.kelas.update', $kelas->id) }}" method="POST">
@csrf
@method('PUT')
<div class="mb-4">
<label class="block mb-1 font-medium">Nama Kelas</label>
<input type="text" name="nama_kelas" value="{{ $kelas->nama_kelas }}" class="w-full border px-4 py-2 rounded" required>
</div>
<div class="mb-4">
<label class="block mb-1 font-medium">Jurusan</label>
<select name="id_jurusan" class="w-full border px-4 py-2 rounded" required>
<option value="">-- Pilih Jurusan --</option>
@foreach($jurusan as $item)
<option value="{{ $item->id }}" {{ $kelas->id_jurusan == $item->id ? 'selected' : '' }}>
{{ $item->nama_jurusan }}
</option>
@endforeach
</select>
</div>
<div class="flex justify-end">
<a href="{{ route('admin.kelas.index') }}" class="bg-gray-300 px-4 py-2 rounded mr-2">Batal</a>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">Update</button>
</div>
</form>
</div>
@endsection

View File

@ -7,7 +7,7 @@
<div class="bg-white shadow-md rounded-lg p-6">
<div class="flex justify-between items-center mb-4">
<h1 class="text-2xl font-semibold">Data Kelas</h1>
<a href="{{ route('siswa.create') }}" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg">
<a href="{{ route('admin.kelas.create') }}" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg">
+ Tambah Kelas
</a>
</div>
@ -27,10 +27,12 @@
<tr class="border hover:bg-gray-50">
<td class="border px-4 py-2">{{ $index + 1 }}</td>
<td class="border px-4 py-2">{{ $item->nama_kelas }}</td>
<td class="border px-4 py-2">{{ $item->nama_jurusan }}</td>
<td class="border px-4 py-2">
{{ $item->jurusan ? $item->jurusan->nama_jurusan : '-' }}
</td>
<td class="border px-4 py-2 text-center">
<a href="{{ route('siswa.edit', $item->id) }}" class="bg-yellow-400 hover:bg-yellow-500 text-white px-3 py-1 rounded">Edit</a>
<form action="{{ route('siswa.destroy', $item->id) }}" method="POST" class="inline-block">
<a href="{{ route('admin.kelas.edit', $item->id) }}" class="bg-yellow-400 hover:bg-yellow-500 text-white px-3 py-1 rounded">Edit</a>
<form action="{{ route('admin.kelas.destroy', $item->id) }}" method="POST" class="inline-block">
@csrf
@method('DELETE')
<button type="submit" onclick="return confirm('Yakin ingin menghapus?')" class="bg-red-500 hover:bg-red-600 text-white px-3 py-1 rounded">
@ -45,4 +47,4 @@
</div>
</div>
@endsection
@endsection

View File

@ -45,8 +45,16 @@
Route::get('/guru', [GuruController::class, 'index'])->name('admin.guru');
// Kelas
Route::get('/kelas', [KelasController::class, 'index'])->name('admin.kelas');
Route::resource('kelas', KelasController::class)->parameters([
'kelas' => 'kelas',
])->names([
'index' => 'admin.kelas.index',
'create' => 'admin.kelas.create',
'store' => 'admin.kelas.store',
'edit' => 'admin.kelas.edit',
'update' => 'admin.kelas.update',
'destroy' => 'admin.kelas.destroy',
]);
// Jurusan
Route::resource('jurusan', JurusanController::class)->names([
'index' => 'admin.jurusan.index',