Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bhojkamal's avatar

How to save save form Data and with multiple input image and big form data Laravel 8 & Vue.js 3

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?

0 likes
1 reply
tchury's avatar

I think the best thing could be a sort of "autosave" function if you have a big quantity of fields. For example, when a user fills an input you save this data, after x seconds or when he left the field.

In this way, you will have the function saveField(paramName, paramValue). I suggest not to save data into the controller, but to create a function setPROPERTYNAME(paramValue) (one for each property) into the model, and call it in the saveField function created before. So, you can manage the error handling in the function setPROPERTYNAME, and your code would be more powerful and readable.

Please or to participate in this conversation.