65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Position;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class PositionController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return Inertia::render('admin/positions/index', [
|
|
'positions' => Position::withCount('employees')->latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('admin/positions/create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255|unique:positions,name',
|
|
]);
|
|
|
|
Position::create($validated);
|
|
|
|
return redirect()->to('/admin/positions')
|
|
->with('success', 'Jabatan berhasil ditambahkan.');
|
|
}
|
|
|
|
public function edit(Position $position)
|
|
{
|
|
return Inertia::render('admin/positions/edit', [
|
|
'position' => $position
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Position $position)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255|unique:positions,name,' . $position->id,
|
|
]);
|
|
|
|
$position->update($validated);
|
|
|
|
return redirect()->to('/admin/positions')
|
|
->with('success', 'Jabatan berhasil diperbarui.');
|
|
}
|
|
|
|
public function destroy(Position $position)
|
|
{
|
|
if ($position->employees()->count() > 0) {
|
|
return back()->with('error', 'Gagal hapus! Masih ada karyawan dengan jabatan ini.');
|
|
}
|
|
|
|
$position->delete();
|
|
|
|
return back()->with('success', 'Jabatan berhasil dihapus.');
|
|
}
|
|
} |