Hello, I have managed to save my data from form which has more than 60 fields with add more fields and data will be saved in multiple tables. I have used Laravel 8 API and Vue.js 3.
Still I am looking is there is more eloquent.
On controller I have like this
public function store(Request $request)
{
$followupData = json_decode($request->followup, true);
$medicationData = json_decode($request->medications, true);
$prescriptionData = json_decode($request->prescriptions, true);
$labreportData = json_decode($request->labreports, true);
$ECG_up_report=[];
$echo_up_report=[];
$x_ray_up_report=[];
$usg_up_report=[];
try{
if($request->hasFile('ecgR')) {
$file = $request->file('ecgR');
$file_name = time().'_'.$file->getClientOriginalName();
$file->move(public_path('images/reports'), $file_name);
$ECG_up_report = ['ECG_up_report' => $file_name];
}
}catch(\Exception $e) {
return response()->json(['message'=>$e->getMessage()]);
}
try{
if($request->hasFile('echoR')) {
$file = $request->file('echoR');
$file_name = time().'_'.$file->getClientOriginalName();
$file->move(public_path('images/reports'), $file_name);
$echo_up_report = ['echo_up_report' => $file_name];
}
}catch(\Exception $e) {
return response()->json(['message'=>$e->getMessage()]);
}
try{
if($request->hasFile('xrayR')) {
$file = $request->file('xrayR');
$file_name = time().'_'.$file->getClientOriginalName();
$file->move(public_path('images/reports'), $file_name);
$x_ray_up_report = ['x_ray_up_report' => $file_name];
}
}catch(\Exception $e) {
return response()->json(['message'=>$e->getMessage()]);
}
try{
if($request->hasFile('usgR')) {
$file = $request->file('usgR');
$file_name = time().'_'.$file->getClientOriginalName();
$file->move(public_path('images/reports'), $file_name);
$usg_up_report = ['usg_up_report' => $file_name];
}
}catch(\Exception $e) {
return response()->json(['message'=>$e->getMessage()]);
}
$followup = array_merge($followupData,$ECG_up_report,$echo_up_report,$x_ray_up_report,$usg_up_report);
$followupSave = Followup::create($followup);
$followupSave->medication()->createMany($medicationData);
$followupSave->prescription()->createMany($prescriptionData);
$followupSave->labreport()->createMany($labreportData);
if ($followupSave->id){
$success = true;
$message = 'Followup data added successfully';
} else {
$success = false;
$message = 'Failed to add followup data';
}
$response = [
'success' => $success,
'message' => $message,
];
return response()->json([$response]);
}
How can I manage better way to manage the error handling too?