query('q', '')); $query = Pegawai::query()->orderByDesc('id'); if ($q !== '') { $query->where(function ($qq) use ($q) { $qq->where('nama', 'like', "%{$q}%") ->orWhere('nip', 'like', "%{$q}%") ->orWhere('nik', 'like', "%{$q}%") ->orWhere('jabatan', 'like', "%{$q}%") ->orWhere('telepon', 'like', "%{$q}%"); }); } return response()->json([ 'data' => $query->get([ 'id', 'nip', 'nama', 'jabatan', 'telepon', 'alamat', 'tempat_lahir', 'tanggal_lahir', 'jenis_kelamin', 'kota', 'nik', 'foto', 'is_aktif', 'created_at', ]), ]); } public function store(Request $request): JsonResponse { $data = $request->validate([ 'nip' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nip')], 'nama' => ['required', 'string', 'max:100'], 'jabatan' => ['nullable', 'string', 'max:100'], 'telepon' => ['nullable', 'string', 'max:30'], 'alamat' => ['nullable', 'string', 'max:255'], 'tempat_lahir' => ['nullable', 'string', 'max:100'], 'tanggal_lahir' => ['nullable', 'date'], 'jenis_kelamin' => ['nullable', 'string', 'max:10'], 'kota' => ['nullable', 'string', 'max:100'], 'nik' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nik')], 'foto' => ['nullable', 'string', 'max:255'], 'is_aktif' => ['nullable', 'boolean'], ]); $pegawai = Pegawai::create([ 'nip' => $data['nip'] ?? null, 'nama' => $data['nama'], 'jabatan' => $data['jabatan'] ?? null, 'telepon' => $data['telepon'] ?? null, 'alamat' => $data['alamat'] ?? null, 'tempat_lahir' => $data['tempat_lahir'] ?? null, 'tanggal_lahir' => $data['tanggal_lahir'] ?? null, 'jenis_kelamin' => $data['jenis_kelamin'] ?? null, 'kota' => $data['kota'] ?? null, 'nik' => $data['nik'] ?? null, 'foto' => $data['foto'] ?? null, 'is_aktif' => (bool) ($data['is_aktif'] ?? true), ]); return response()->json(['data' => $pegawai], 201); } public function update(Request $request, Pegawai $pegawai): JsonResponse { $data = $request->validate([ 'nip' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nip')->ignore($pegawai->id)], 'nama' => ['required', 'string', 'max:100'], 'jabatan' => ['nullable', 'string', 'max:100'], 'telepon' => ['nullable', 'string', 'max:30'], 'alamat' => ['nullable', 'string', 'max:255'], 'tempat_lahir' => ['nullable', 'string', 'max:100'], 'tanggal_lahir' => ['nullable', 'date'], 'jenis_kelamin' => ['nullable', 'string', 'max:10'], 'kota' => ['nullable', 'string', 'max:100'], 'nik' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nik')->ignore($pegawai->id)], 'foto' => ['nullable', 'string', 'max:255'], 'is_aktif' => ['nullable', 'boolean'], ]); $pegawai->update([ 'nip' => $data['nip'] ?? null, 'nama' => $data['nama'], 'jabatan' => $data['jabatan'] ?? null, 'telepon' => $data['telepon'] ?? null, 'alamat' => $data['alamat'] ?? null, 'tempat_lahir' => $data['tempat_lahir'] ?? null, 'tanggal_lahir' => $data['tanggal_lahir'] ?? null, 'jenis_kelamin' => $data['jenis_kelamin'] ?? null, 'kota' => $data['kota'] ?? null, 'nik' => $data['nik'] ?? null, 'foto' => $data['foto'] ?? null, 'is_aktif' => (bool) ($data['is_aktif'] ?? true), ]); return response()->json(['data' => $pegawai->fresh()]); } public function destroy(Pegawai $pegawai): JsonResponse { $pegawai->delete(); return response()->json(['message' => 'Pegawai dihapus.']); } public function uploadFoto(Request $request): JsonResponse { $data = $request->validate([ 'foto' => ['required', 'image', 'max:2048'], ]); $path = $data['foto']->store('pegawai', 'public'); $url = Storage::url($path); return response()->json(['data' => ['foto' => $url]]); } }