diff --git a/app/Http/Controllers/HealthDetailsController.php b/app/Http/Controllers/HealthDetailsController.php index 6c14d56..73df8eb 100644 --- a/app/Http/Controllers/HealthDetailsController.php +++ b/app/Http/Controllers/HealthDetailsController.php @@ -3,10 +3,11 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Models\User; -use App\Models\HealthDetails; use Illuminate\Support\Facades\Auth; +use App\Models\HealthDetails; +use Illuminate\Support\Facades\Request as FacadesRequest; use Illuminate\Support\Facades\Validator; +use RealRashid\SweetAlert\Facades\Alert; class HealthDetailsController extends Controller { @@ -18,10 +19,7 @@ class HealthDetailsController extends Controller public function index() { $user = Auth::user(); - $healthDetails = HealthDetails::with('user')->get(); - - // Fetch the authenticated user's data with profile picture - $user = User::find($user->id); + $healthDetails = HealthDetails::where('userId', $user->id)->get(); return view('health-details.index', compact('healthDetails', 'user')); } @@ -34,21 +32,82 @@ public function index() */ public function addHealthData(Request $request) { - $request->validate([ + $validator = Validator::make($request->all(), [ 'userId' => 'required|exists:users,id', - 'physical_disability' => 'nullable|string|max:255', - 'blood_group' => 'nullable|string|max:10', - 'illness_history' => 'nullable|string', - 'health_insurance' => 'nullable|string|max:255', - 'insur_name' => 'nullable|string|max:255', - 'insur_no' => 'nullable|string|max:50', - 'allergies' => 'nullable|string', - 'delete_status' => 'nullable|boolean', + 'physical_disability' => 'required', + 'health_insurance' => 'required', + ]); - $healthDetail = HealthDetails::create($request->all()); + if ($validator->fails()) { + return response()->json([ + 'status' => 400, + 'error' => $validator->errors() + ]); + } + + HealthDetails::create([ + 'userId' => $request->input('userId'), + 'physical_disability' => $request->input('physical_disability'), + 'blood_group' => $request->input('blood_group'), + 'illness_history' => $request->input('illness_history'), + 'health_insurance' => $request->input('health_insurance'), + 'insur_name' => $request->input('insur_name'), + 'insur_no' => $request->input('insur_no'), + 'allergies' => $request->input('allergies'), + 'delete_status' =>0, + ]); - return redirect()->route('health-details.index') - ->with('success', 'Health detail added successfully.'); + return redirect()->route('health-details.index')->with('success', 'Health details added successfully.'); } + + public function edit(string $id){ + $healthData = HealthDetails::findOrFail($id); + return view('health-details.edit', compact('healthData')); + } + + public function updateHealthDetails(Request $request, string $id){ + + $validator = Validator::make($request->all(),[ + 'physical_disability' => 'required', + 'health_insurance' => 'required', + ]); + + if ($validator->fails()) { + return response()->json([ + 'status' => 400, + 'errors' => $validator->errors(), + ]); + } + + $healthData = HealthDetails::findOrFail($id); + $healthData->update([ + 'physical_disability' => $request->input('physical_disability'), + 'blood_group' => $request->input('blood_group'), + 'illness_history' => $request->input('illness_history'), + 'health_insurance' => $request->input('health_insurance'), + 'insur_name' => $request->input('insur_name'), + 'insur_no' => $request->input('insur_no'), + 'allergies' => $request->input('allergies'), + ]); + Alert::success('Health detail Updated successful','Health details updated'); + return redirect()->route('health-details.index')->with('success', 'Department updated successfully.'); + + } + + public function deleteHealthData(string $id){ + $health = HealthDetails::find($id); + + if(!$health){ + return response()->json([ + 'status' => 400, + 'massage' => ' Health detail not found', + ]); + } + + $health->update([ + 'delete_status' => 1 + ]); + } + } diff --git a/app/Http/Controllers/UserFamilyDetailsController.php b/app/Http/Controllers/UserFamilyDetailsController.php index 695cdc7..6e7682c 100644 --- a/app/Http/Controllers/UserFamilyDetailsController.php +++ b/app/Http/Controllers/UserFamilyDetailsController.php @@ -16,36 +16,36 @@ public function index() return view('family-details.index', compact('user', 'familyData')); } - public function addFamilyData(Request $request) { $validator = Validator::make($request->all(), [ - 'userId' => 'required|exists:users,id', 'familyData.*.full_name' => 'required|string|max:255', 'familyData.*.relationship' => 'required|string|max:255', - 'familyData.*.phone_number' => 'required|string|max:15', + 'familyData.*.phone_number' => 'nullable|string|max:15', 'familyData.*.occupation' => 'nullable|string|max:255', + 'familyData.*.DOB' => 'nullable|date', ]); - + if ($validator->fails()) { return redirect()->back()->withErrors($validator)->withInput(); } - $userId =Auth::user(); - //dd($userId); + + $userId = Auth::id(); // Get authenticated user's ID + foreach ($request->familyData as $data) { UserFamilyDetails::create([ - 'userId' => $userId , + 'userId' => $userId, 'full_name' => $data['full_name'], 'relationship' => $data['relationship'], 'phone_number' => $data['phone_number'], - 'DOB' => $data['DOB'], 'occupation' => $data['occupation'], + 'DOB' => $data['DOB'], ]); } - - return redirect()->route('profile.show')->with('success', 'Family details added successfully.'); + + return redirect()->route('family-details.index')->with('success', 'Family details added successfully.'); } - + public function addHealthData(Request $request) { $validator = Validator::make($request->all(), [ diff --git a/resources/views/health-details/create.blade.php b/resources/views/health-details/create.blade.php index 5237e0f..236a27e 100644 --- a/resources/views/health-details/create.blade.php +++ b/resources/views/health-details/create.blade.php @@ -1,80 +1,234 @@ -