TIFNJK_E41222887/app/Http/Controllers/Admin/EmployeeController.php

134 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\Employee;
use App\Models\Position;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Inertia\Inertia;
class EmployeeController extends Controller
{
public function index(Request $request)
{
$query = Employee::with(['user', 'department', 'position']);
if ($request->search) {
$query->where('nip', 'like', '%' . $request->search . '%')
->orWhereHas('user', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%');
});
}
if ($request->department_id) {
$query->where('department_id', $request->department_id);
}
return Inertia::render('admin/employees/index', [
'employees' => $query->latest()->paginate(10)->withQueryString(),
'departments' => Department::all(),
'filters' => $request->only(['search', 'department_id']),
]);
}
public function create()
{
return Inertia::render('admin/employees/create', [
'departments' => Department::all(),
'positions' => Position::all(),
]);
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed',
'nip' => 'required|string|unique:employees,nip',
'gender' => 'required|in:L,P',
'place_of_birth' => 'nullable|string|max:255',
'birth_date' => 'required|date',
'address' => 'nullable|string',
'phone_number' => 'nullable|string',
'department_id' => 'required|exists:departments,id',
'position_id' => 'required|exists:positions,id',
'status' => 'required|in:PKWT,PKWTT,Magang',
'join_date' => 'required|date',
]);
$user = User::create([
'name' => $validated['name'],
'email' => $validated['email'],
'password' => Hash::make($validated['password']),
]);
Employee::create([
'user_id' => $user->id,
'nip' => $validated['nip'],
'name' => $validated['name'],
'gender' => $validated['gender'],
'place_of_birth' => $validated['place_of_birth'],
'birth_date' => $validated['birth_date'],
'address' => $validated['address'],
'phone_number' => $validated['phone_number'],
'department_id' => $validated['department_id'],
'position_id' => $validated['position_id'],
'status' => $validated['status'],
'join_date' => $validated['join_date'],
]);
return redirect()->route('admin.employees.index')
->with('success', 'Data karyawan berhasil ditambahkan.');
}
public function edit(Employee $employee)
{
$employee->load(['user', 'department', 'position']);
return Inertia::render('admin/employees/edit', [
'employee' => $employee,
'departments' => Department::all(),
'positions' => Position::all(),
]);
}
public function update(Request $request, Employee $employee)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => ['required', 'email', Rule::unique('users')->ignore($employee->user_id)],
'nip' => ['required', 'string', Rule::unique('employees')->ignore($employee->id)],
'gender' => 'required|in:L,P',
'place_of_birth' => 'nullable|string|max:255',
'birth_date' => 'required|date',
'address' => 'nullable|string',
'phone_number' => 'nullable|string',
'department_id' => 'required|exists:departments,id',
'position_id' => 'required|exists:positions,id',
'status' => 'required|in:PKWT,PKWTT,Magang',
'join_date' => 'required|date',
]);
$employee->user->update([
'name' => $validated['name'],
'email' => $validated['email'],
]);
$employee->update($validated);
return redirect()->route('admin.employees.index')
->with('success', 'Data karyawan diperbarui.');
}
public function destroy(Employee $employee)
{
$employee->user->delete();
return back()->with('success', 'Karyawan dihapus.');
}
}