validate([ 'username' => 'required|string', 'password' => 'required|string', 'pin' => 'required|numeric', ]); $student = DB::table('student_portal_logs') ->where('email', $request->username) ->orWhere('studentid', $request->username) ->first(); if ($student && Hash::check($request->password, $student->password) && $student->pin == $request->pin) { if ($student->status !== 'active' && $student->status !== '' && !is_null($student->status)) { return redirect()->back()->with('error', 'Your account is inactive!'); } $request->session()->put('student_id', $student->studentid); $request->session()->put('student_log_id', $student->id ?? null); $name = trim(($student->first_name ?? '') . ' ' . ($student->last_name ?? '')); $request->session()->put('student_name', $name ?: ($student->email ?? $student->studentid)); return redirect()->route('student.portal')->with('success', 'Logged in successfully!'); } return redirect()->back()->with('error', 'Invalid Email/Username, Password, or PIN!'); } public function showProfile(Request $request) { $log_id = $request->session()->get('student_log_id'); $student_code = $request->session()->get('student_id'); if (!$log_id && !$student_code) { return redirect('/')->with('error', 'Please login first!'); } $student = DB::table('student_details') ->where(function ($query) use ($log_id, $student_code) { if ($log_id) { $query->where('student_portal_log_id', $log_id); } if ($student_code) { if ($log_id) { $query->orWhere('student_id', $student_code); } else { $query->where('student_id', $student_code); } } }) ->first(); if (!$student) { return redirect()->route('student.portal')->with('error', 'Student details not found in database!'); } return view('StudentProfile', compact('student')); } public function updateProfile(Request $request) { $log_id = $request->session()->get('student_log_id'); $student_code = $request->session()->get('student_id'); if (!$log_id && !$student_code) { return redirect('/')->with('error', 'Please login first!'); } $request->validate([ 'full_name' => 'required|string|max:255', 'nic_passport' => 'nullable|string|max:100', 'date_of_birth' => 'nullable|date', 'gender' => 'nullable|string|max:20', 'email' => 'required|email|max:255', 'phone' => 'nullable|string|max:50', 'qualification' => 'nullable|string|max:255', 'institute' => 'nullable|string|max:255', 'year_completed' => 'nullable|string|max:50', 'profile_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:5120', ]); $student = DB::table('student_details') ->where(function ($query) use ($log_id, $student_code) { if ($log_id) { $query->where('student_portal_log_id', $log_id); } if ($student_code) { if ($log_id) { $query->orWhere('student_id', $student_code); } else { $query->where('student_id', $student_code); } } }) ->first(); if (!$student) { return redirect()->back()->with('error', 'Student profile record not found!'); } $updateData = [ 'full_name' => $request->full_name, 'nic_passport' => $request->nic_passport, 'date_of_birth' => $request->date_of_birth, 'gender' => $request->gender, 'email' => $request->email, 'phone' => $request->phone, 'qualification' => $request->qualification, 'institute' => $request->institute, 'year_completed' => $request->year_completed, 'updated_at' => now(), ]; // 1. Check if user requested to REMOVE image if ($request->input('remove_profile_image') == '1') { if (!empty($student->image) && file_exists(public_path($student->image))) { @unlink(public_path($student->image)); } $updateData['image'] = null; } // 2. Check if user provided CROPPED Base64 image elseif (!empty($request->input('cropped_image_data'))) { $base64Image = $request->input('cropped_image_data'); if (preg_match('/^data:image\/(\w+);base64,/', $base64Image, $type)) { $data = substr($base64Image, strpos($base64Image, ',') + 1); $type = strtolower($type[1]); if (in_array($type, ['jpg', 'jpeg', 'gif', 'png'])) { $data = base64_decode($data); if ($data !== false) { $filename = time() . '_' . uniqid() . '.png'; $destinationPath = public_path('uploads/student_images'); if (!file_exists($destinationPath)) { mkdir($destinationPath, 0777, true); } file_put_contents($destinationPath . '/' . $filename, $data); // Delete old image if existed if (!empty($student->image) && file_exists(public_path($student->image))) { @unlink(public_path($student->image)); } $updateData['image'] = 'uploads/student_images/' . $filename; } } } } // 3. Fallback to normal direct file upload elseif ($request->hasFile('profile_image')) { $file = $request->file('profile_image'); $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension(); $destinationPath = public_path('uploads/student_images'); if (!file_exists($destinationPath)) { mkdir($destinationPath, 0777, true); } $file->move($destinationPath, $filename); if (!empty($student->image) && file_exists(public_path($student->image))) { @unlink(public_path($student->image)); } $updateData['image'] = 'uploads/student_images/' . $filename; } DB::table('student_details') ->where('id', $student->id) ->update($updateData); if (!empty($student->student_portal_log_id)) { DB::table('student_portal_logs') ->where('id', $student->student_portal_log_id) ->update([ 'email' => $request->email, 'updated_at' => now(), ]); } $request->session()->put('student_name', $request->full_name); $request->session()->put('student_email', $request->email); return redirect()->back()->with('success', 'Profile updated successfully!'); } }