first commit
This commit is contained in:
55
app/Http/Controllers/Admin/VotController.php
Normal file
55
app/Http/Controllers/Admin/VotController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Jabatan;
|
||||
use App\Models\Vot;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VotController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$vot = Vot::with('jabatan')
|
||||
->when($request->search, fn ($q, $v) => $q->where('nama', 'like', "%$v%")->orWhere('kod', 'like', "%$v%"))
|
||||
->when($request->jabatan_id, fn ($q, $v) => $q->where('jabatan_id', $v))
|
||||
->latest()->paginate(10)->withQueryString();
|
||||
$jabatan = Jabatan::orderBy('nama')->get();
|
||||
|
||||
return view('admin.vot.index', compact('vot', 'jabatan'));
|
||||
}
|
||||
|
||||
public function create() { return view('admin.vot.form', ['vot' => new Vot(), 'jabatan' => Jabatan::orderBy('nama')->get()]); }
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
Vot::create($this->validated($request));
|
||||
return redirect()->route('vot.index')->with('success', 'VOT berjaya ditambah.');
|
||||
}
|
||||
|
||||
public function edit(Vot $vot) { return view('admin.vot.form', ['vot' => $vot, 'jabatan' => Jabatan::orderBy('nama')->get()]); }
|
||||
|
||||
public function update(Request $request, Vot $vot)
|
||||
{
|
||||
$vot->update($this->validated($request, $vot->id));
|
||||
return redirect()->route('vot.index')->with('success', 'VOT berjaya dikemaskini.');
|
||||
}
|
||||
|
||||
public function destroy(Vot $vot)
|
||||
{
|
||||
$vot->delete();
|
||||
return back()->with('success', 'VOT berjaya dipadam.');
|
||||
}
|
||||
|
||||
private function validated(Request $request, ?int $id = null): array
|
||||
{
|
||||
return $request->validate([
|
||||
'kod' => ['required', 'max:50', 'unique:vot,kod,'.$id],
|
||||
'nama' => ['required', 'max:255'],
|
||||
'jabatan_id' => ['nullable', 'exists:jabatan,id'],
|
||||
'penerangan' => ['nullable'],
|
||||
'status' => ['nullable', 'boolean'],
|
||||
]) + ['status' => $request->boolean('status')];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user