EVOLUTION-NINJA
Edit File: StudentController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use App\Models\Register; use App\Models\add_student; use App\Models\admin; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rule; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use App\Models\LoginLog; use App\Models\EmailOtp; use App\Mail\OtpMail; use Illuminate\Support\Facades\Mail; use Stevebauman\Location\Facades\Location; use Illuminate\Support\Facades\Cookie; class StudentController extends Controller { public function Add_student(Request $request) { $validated = $request->validate([ 'firstName' => 'nullable|string', 'middleName' => 'nullable|string', 'lastName' => 'nullable|string', 'dob' => 'nullable|date', 'gender' => 'nullable|string', 'nationality' => 'nullable|string', 'bloodGroup' => 'nullable|string', 'religion' => 'nullable|string', 'category' => 'nullable|string', 'city' => 'nullable|string', 'phone' => 'nullable|string', 'email' => 'nullable|email', 'fatherName' => 'nullable|string', 'motherName' => 'nullable|string', 'parentContact' => 'nullable|string', 'parentsEmail' => 'nullable|string', 'state' => 'nullable|string', 'address' => 'nullable|string', 'admissionDate' => 'nullable|date', 'rollNumber' => 'nullable|string', 'academicYear' => 'nullable|string', 'previousSchool' => 'nullable|string', 'currentClass' => 'nullable|string', 'section' => 'nullable|string', 'admissionType' => 'nullable|string', 'transferCertificate' => 'nullable|string', 'photo' => 'nullable|file|image|max:2048' ]); DB::beginTransaction(); try { // ✅ Handle photo upload if ($request->hasFile('photo')) { $path = $request->file('photo')->store('students', 'public'); $validated['photo'] = $path; } // ✅ Generate Student ID $lastStudent = DB::table('add_students')->orderBy('id', 'desc')->first(); $nextId = $lastStudent ? $lastStudent->id + 1 : 1; $year = date('Y'); $studentId = 'STU' . $year . str_pad($nextId, 5, '0', STR_PAD_LEFT); $student_id_in_db = DB::table('add_students')->insertGetId([ 'student_id' => $studentId, 'first_name' => $validated['firstName'], 'middle_name' => $validated['middleName'], 'last_name' => $validated['lastName'], 'role' => 3, 'DOB' => $validated['dob'], 'Gender' => $validated['gender'], 'phone' => $validated['phone'], 'email' => $validated['email'], 'admissionDate' => $validated['admissionDate'], 'academicYear' => $validated['academicYear'], 'currentClass' => $validated['currentClass'], 'section' => $validated['section'], 'photo' => $validated['photo'] ?? null, 'created_at' => now(), 'updated_at' => now(), ]); // Insert into student_additional_details DB::table('student_additional_details')->insert([ 'student_id' => $student_id_in_db , 'Nationality' => $validated['nationality'], 'Blood_group' => $validated['bloodGroup'], 'religion' => $validated['religion'], 'category' => $validated['category'], 'fatherName' => $validated['fatherName'], 'motherName' => $validated['motherName'], 'parentContact' => $validated['parentContact'], 'parentEmail' => $validated['parentsEmail'], 'address' => $validated['address'], 'city' => $validated['city'], 'state' => $validated['state'], 'previousSchool' => $validated['previousSchool'], 'admissionType' => $validated['admissionType'], 'transferCertificate' => $validated['transferCertificate'], ]); // Create parent login $defaultPassword = bcrypt('parent@123'); DB::table('users')->insert([ 'name' => $validated['fatherName'] ?? 'Parent of ' . $studentId, 'email' => $validated['parentsEmail'], 'password' => $defaultPassword, 'role' => 4, 'created_at' => now(), 'updated_at' => now(), ]); DB::commit(); return response()->json([ 'message' => 'Student and parent accounts created successfully!', 'student_id' => $studentId, ], 201); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'message' => 'Failed to add student: ' . $e->getMessage() ], 500); } } public function StudentList() { $students = add_student::orderBy('id', 'desc')->get(); return response()->json([ 'students' => $students ]); } public function logout(Request $request) { // Revoke current token $request->user()->currentAccessToken()->delete(); return response()->json([ 'message' => 'Logged out successfully.' ]); } public function studentsEdit($id) { $student = add_student::where('student_id', $id)->firstOrFail(); return response()->json(['student' => $student]); } public function StudentsUpdate(Request $request, $student_id) { \Log::info("Updating student_id: {$student_id}"); \Log::info("Request all: ", $request->all()); \Log::info("Request files: ", $request->file()); $student = DB::table('add_students') ->where('student_id', $student_id) ->first(); if (!$student) { return response()->json(['message' => 'Student not found'], 404); } $updateData = [ 'first_name' => $request->input('firstName'), 'middle_name' => $request->input('middleName'), 'last_name' => $request->input('lastName'), 'dob' => $request->input('dob'), 'gender' => $request->input('gender'), 'nationality' => $request->input('nationality'), 'blood_group' => $request->input('bloodGroup'), 'religion' => $request->input('religion'), 'category' => $request->input('category'), 'city' => $request->input('city'), 'phone' => $request->input('phone'), 'email' => $request->input('email'), 'fatherName' => $request->input('fatherName'), 'motherName' => $request->input('motherName'), 'parentContact' => $request->input('parentContact'), 'parentsEmail' => $request->input('parentsEmail'), 'state' => $request->input('state'), 'address' => $request->input('address'), 'admissionDate' => $request->input('admissionDate'), 'academicYear' => $request->input('academicYear'), 'previousSchool' => $request->input('previousSchool'), 'currentClass' => $request->input('currentClass'), 'section' => $request->input('section'), 'admissionType' => $request->input('admissionType'), 'transferCertificate' => $request->input('transferCertificate'), ]; // Handle file upload if ($request->hasFile('photo')) { $path = $request->file('photo')->store('photos', 'public'); $updateData['photo'] = $path; } DB::table('add_students') ->where('student_id', $student_id) ->update($updateData); return response()->json(['message' => 'Student updated successfully']); } public function nextStudentId() { $lastStudent = add_student::orderBy('id', 'desc')->first(); $lastId = $lastStudent ? intval(substr($lastStudent->student_id, -4)) : 0; $nextIdNum = $lastId + 1; $nextIdStr = str_pad($nextIdNum, 4, '0', STR_PAD_LEFT); $year = now()->year; $nextStudentId = "STU{$year}{$nextIdStr}"; return response()->json([ 'nextStudentId' => $nextStudentId, ]); } public function ViewStudent($id) { $student = add_student::where('student_id', $id)->first(); if (!$student) { return response()->json(['message' => 'Student not found'], 404); } return response()->json([ 'student' => $student ]); } public function StudentsDelete($id) { $student = add_student::where('student_id', $id)->firstOrFail(); $student->delete(); return response()->json(['message' => 'Student deleted successfully']); } public function storeAttendance(Request $request) { $attendanceData = $request->input('attendance'); if (!$attendanceData || !is_array($attendanceData)) { return response()->json(['message' => 'Invalid data format.'], 400); } foreach ($attendanceData as $entry) { if (!isset($entry['student_id']) || !isset($entry['status']) || !isset($entry['class'])) { continue; } $existing = DB::table('attendance') ->where('student_id', $entry['student_id']) ->where('class', $entry['class']) ->whereDate('date', now()->format('Y-m-d')) ->first(); if ($existing) { // Update existing DB::table('attendance') ->where('id', $existing->id) ->update([ 'status' => $entry['status'], 'updated_at' => now(), ]); } else { // Insert new DB::table('attendance')->insert([ 'student_id' => $entry['student_id'], 'class' => $entry['class'], 'date' => now()->format('Y-m-d'), 'status' => $entry['status'], 'created_at' => now(), 'updated_at' => now(), ]); } } return response()->json(['message' => 'Attendance submitted successfully.'], 200); } // attendance public function students_attendance() { $students = add_student::select('id', 'first_name', 'last_name', 'currentClass', 'student_id as admission_no') ->get() ->map(function ($student) { return [ 'id' => $student->id, 'name' => trim($student->first_name . ' ' . $student->last_name), 'admission_no' => $student->admission_no, 'cls' => $student->currentClass, ]; }); return response()->json(['students' => $students]); } public function getAttendanceReport() { $attendance = DB::table('attendance')->orderBy('date', 'desc')->get(); if ($attendance->isEmpty()) { return response()->json(['message' => 'No attendance found.'], 404); } return response()->json(['attendance' => $attendance], 200); } }