89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\PaketMapel;
|
|
use App\Models\Mapel;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class PaketMapelController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$pakets = PaketMapel::with('mapels')->get();
|
|
return view('admin.angket.paket.index', compact('pakets'))
|
|
->with('success', session('success'))
|
|
->withErrors(session('error'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$mapels = Mapel::all();
|
|
return view('admin.angket.paket.create', compact('mapels'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'nama_paket' => 'required',
|
|
'deskripsi' => 'required',
|
|
'kuota' => 'required|integer|min:0',
|
|
'mapel_id' => 'required|array'
|
|
]);
|
|
|
|
try {
|
|
$paket = PaketMapel::create($request->only(['nama_paket', 'deskripsi', 'kuota']));
|
|
$paket->mapels()->attach($request->mapel_id);
|
|
|
|
return redirect()->route('admin.angket.paket.index')->with('success', 'Paket berhasil ditambahkan');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal menyimpan paket: ' . $e->getMessage());
|
|
return back()->withInput()->withErrors('Terjadi kesalahan saat menambahkan paket.');
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$paket = PaketMapel::findOrFail($id);
|
|
$mapels = Mapel::all();
|
|
return view('admin.angket.paket.edit', compact('paket', 'mapels'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'nama_paket' => 'required',
|
|
'deskripsi' => 'required',
|
|
'kuota' => 'required|integer|min:0',
|
|
'mapel_id' => 'required|array'
|
|
]);
|
|
|
|
try {
|
|
$paket = PaketMapel::findOrFail($id);
|
|
$paket->update($request->only(['nama_paket', 'deskripsi', 'kuota']));
|
|
$paket->mapels()->sync($request->mapel_id);
|
|
|
|
return redirect()->route('admin.paket-mapel.index')->with('success', 'Paket berhasil diperbarui');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal memperbarui paket: ' . $e->getMessage());
|
|
return back()->withInput()->withErrors('Terjadi kesalahan saat memperbarui paket.');
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$paket = PaketMapel::findOrFail($id);
|
|
$paket->mapels()->detach();
|
|
$paket->delete();
|
|
|
|
return back()->with('success', 'Paket berhasil dihapus');
|
|
} catch (\Exception $e) {
|
|
Log::error('Gagal menghapus paket: ' . $e->getMessage());
|
|
return back()->withErrors('Terjadi kesalahan saat menghapus paket.');
|
|
}
|
|
}
|
|
}
|