121 lines
3.8 KiB
PHP
121 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\KelasMapel;
|
|
use App\Models\Kelas;
|
|
use App\Models\Mapel;
|
|
use App\Models\TahunAjaran;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class KelasMapelController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$tahunAjaranAll = TahunAjaran::orderBy('tahun', 'desc')->get();
|
|
$selectedTahun = $request->input('tahun_ajaran_id');
|
|
|
|
$query = KelasMapel::with(['kelas.tahunAjaran', 'mapel'])
|
|
->when($selectedTahun, function($q) use ($selectedTahun) {
|
|
$q->whereHas('kelas', function($q) use ($selectedTahun) {
|
|
$q->where('tahun_ajaran_id', $selectedTahun);
|
|
});
|
|
})
|
|
->orderBy('created_at', 'desc');
|
|
|
|
$data = $query->paginate(10);
|
|
|
|
return view('admin.kelas_mapel.index', compact('data', 'tahunAjaranAll', 'selectedTahun'))
|
|
->with('success', session('success'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$tahunAktif = TahunAjaran::where('status', 'aktif')->first();
|
|
|
|
$existing = KelasMapel::whereHas('kelas', function($q) use ($tahunAktif) {
|
|
$q->where('tahun_ajaran_id', $tahunAktif->id);
|
|
})
|
|
->select('kelas_id', 'mapel_id')
|
|
->get();
|
|
|
|
$usedCombinations = [];
|
|
foreach ($existing as $e) {
|
|
$usedCombinations[$e->kelas_id][] = $e->mapel_id;
|
|
}
|
|
|
|
return view('admin.kelas_mapel.create', [
|
|
'kelas' => Kelas::where('tahun_ajaran_id', $tahunAktif->id)->get(),
|
|
'mapel' => Mapel::all(),
|
|
'used' => $usedCombinations,
|
|
'tahunAjaran' => TahunAjaran::all(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'kelas_id' => 'required',
|
|
'mapel_id' => 'required',
|
|
'tahun_ajaran_id' => 'required',
|
|
]);
|
|
|
|
try {
|
|
KelasMapel::create($request->all());
|
|
return redirect()->route('admin.kelas_mapel.index')->with('success', 'Data berhasil ditambahkan');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal menambahkan data kelas mapel: ' . $e->getMessage());
|
|
return back()->withInput()->withErrors('Gagal menambahkan data.');
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$tahunAjaranAll = TahunAjaran::all();
|
|
$data = KelasMapel::findOrFail($id);
|
|
$kelasAll = Kelas::all();
|
|
$mapelAll = Mapel::all();
|
|
|
|
return view('admin.kelas_mapel.edit', [
|
|
'data' => $data,
|
|
'kelas' => $kelasAll,
|
|
'mapel' => $mapelAll,
|
|
'tahunAjaran' => $tahunAjaranAll,
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'kelas_id' => 'required',
|
|
'mapel_id' => 'required',
|
|
'tahun_ajaran_id' => 'required',
|
|
]);
|
|
|
|
try {
|
|
$data = KelasMapel::findOrFail($id);
|
|
$data->update($request->all());
|
|
|
|
return redirect()->route('admin.kelas_mapel.index')->with('success', 'Data berhasil diupdate');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal memperbarui data kelas mapel: ' . $e->getMessage());
|
|
return back()->withInput()->withErrors('Gagal memperbarui data.');
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$data = KelasMapel::findOrFail($id);
|
|
$data->delete();
|
|
|
|
return redirect()->back()->with('success', 'Data berhasil dihapus');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal menghapus data kelas mapel: ' . $e->getMessage());
|
|
return redirect()->back()->withErrors('Gagal menghapus data.');
|
|
}
|
|
}
|
|
}
|