validate([ 'kelompok_id' => 'required|exists:kelompok_belajar_tani,id', 'user_id' => [ 'required', Rule::exists('users', 'id')->where('role', Role::PETANI->value), ], 'jabatan' => 'required|string|max:255', ]); $user = User::with('profile') ->where('role', Role::PETANI->value) ->findOrFail($request->user_id); DetailKelompok::create([ 'kelompok_id' => $request->kelompok_id, 'user_id' => $user->id, 'nama_anggota' => $user->name, 'jabatan' => $request->jabatan, 'no_hp' => $user->profile->telepon ?? null, 'alamat' => $user->profile->desakelurahan->nama ?? null, ]); return back()->with( 'success', 'Anggota berhasil ditambahkan.' ); } public function update(Request $request, $id) { $request->validate([ 'user_id' => [ 'required', Rule::exists('users', 'id')->where('role', Role::PETANI->value), ], 'jabatan' => 'required|string|max:255', ]); $anggota = DetailKelompok::findOrFail($id); $user = User::with('profile') ->where('role', Role::PETANI->value) ->findOrFail($request->user_id); $anggota->update([ 'user_id' => $user->id, 'nama_anggota' => $user->name, 'jabatan' => $request->jabatan, 'no_hp' => $user->profile->telepon ?? null, 'alamat' => $user->profile->desakelurahan->nama ?? null, ]); return back()->with( 'success', 'Anggota berhasil diperbarui.' ); } public function destroy($id) { $anggota = DetailKelompok::findOrFail($id); $anggota->delete(); return back()->with('success', 'Anggota berhasil dihapus.'); } public function detail($id) { $kelompok = KelompokBelajarTani::with('detailAnggota.user') ->findOrFail($id); $petani = User::with('profile') ->where('role', Role::PETANI->value) ->orderBy('name') ->get(); return view('program.kelompokbelajar.detailkelompok', compact( 'kelompok', 'petani' )); } }