I have problm of create a update functionality in my student records. The relationship between student and study record is one to many, It mean that one student is allowed to have multiple study record history. I'm already done create functionality in the store controller, But I'm stuck of implement update controller function . I use Laravel 10 with Inertia in React that is very new for me. Help me fix this problem
This is the code in store controller function :
public function store(Request $request)
{
$validated = $request->validate([
'name_eng' => 'required|string|max:50',
'name_kh' => 'required|string|max:50',
'student_id_card' => 'required',
'sex' => 'required|string|max:10',
'nationality' => 'required|string|max:20',
'religion' => 'required|string|max:20',
'dob' => 'required|date',
'pob_eng' => 'required|string|max:255',
'pob_kh' => 'required|string|max:255',
'current_address_eng' => 'required|string|max:255',
'current_address_kh' => 'required|string|max:255',
'personal_phone_number' => 'required|string|max:15',
'have_studied_bcc' => 'required|string|max:10',
'requested_program' => 'required|string',
'requested_session' => 'required|string',
'id_photo' => 'required',
'national_id_photo' => 'required',
'enrollment_status' => 'required|string', // Assuming only these values are allowed
]);
if ($request->hasFile('id_photo')) {
$image = $request->file('id_photo');
$imageName = time() . '_' . $image->getClientOriginalName();
// Additional validation (optional)
if (!$image->isValid()) {
throw new InvalidArgumentException('Invalid image file.');
}
$path = $image->storeAs('student_images', $imageName, 'public');
$validated['id_photo'] = $path;
}
if ($request->hasFile('national_id_photo')) {
$image = $request->file('national_id_photo');
$imageName = time() . '_' . $image->getClientOriginalName();
if (!$image->isValid()) {
throw new InvalidArgumentException('Invalid image file.');
}
$path = $image->storeAs('national_id_images', $imageName, 'public');
$validated['national_id_photo'] = $path;
}
$guardian = Guardian::create([
'father_name_eng' => $request->input('father_name_eng'),
'father_name_kh' => $request->input('father_name_kh'),
'father_phone_number' => $request->input('father_phone_number'),
'mother_name_eng' => $request->input('mother_name_eng'),
'mother_name_kh' => $request->input('mother_name_kh'),
'mother_phone_number' => $request->input('mother_phone_number'),
'guardian_name_eng' => $request->input('guardian_name_eng'),
'guardian_name_kh' => $request->input('guardian_name_kh'),
'guardian_phone_number' => $request->input('guardian_phone_number'),
]);
$student = new Student($validated);
$student-> guardian_id = $guardian->id; // Associate the guardian with the student
$student->save();
$student->programs()->attach($request->program);
$student->levels()->attach($request->level);
$previousStudyRecordData = [];
// Check if the record_type array is not null and is an array
if ($request->has('record_type') && is_array($request->input('record_type'))) {
foreach ($request->input('record_type') as $index => $recordType) {
// Populate $previousStudyRecordData array if the input is not null
if (!is_null($recordType)) {
$previousStudyRecordData[] = [
'record_type' => $recordType,
'have_learnt_program' => $request->input('have_learnt_program')[$index] ?? null,
'have_learnt_at' => $request->input('have_learnt_at')[$index] ?? null,
'grade' => $request->input('grade')[$index] ?? null,
'study_status' => $request->input('study_status')[$index] ?? null,
'study_performance' => $request->input('study_performance')[$index] ?? null,
'if_stop_reason' => $request->input('if_stop_reason')[$index] ?? null,
'student_id' => $student->id,
];
}
}
}
// Insert the previous study records if there are any
if (!empty($previousStudyRecordData)) {
// Use updateOrCreate to handle both creation and updates based on student_id
PreviousStudyRecord::insert(
$previousStudyRecordData
);
}
session()->flash('addStatus', 'Success');
return redirect()->route('student.index');
}