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

bhojkamal's avatar

Storing/updating data to multiple table using eloquent relationship on Laravel 8 api

Hello, I have succeed to store the data in multiple table on laravel 8 using eloquent relationships. But I am not sure is this best way to do.

I have tele patient table and 3 others current_medication, Prescribed_Medication and tab_Investigations - with one to many relationship. patient_id should go on the 3 tables.

Received the data as json ojbect from fontend like this.

Patient Data: { "created_by": 1, "firstname": "Krishna", "lastname": "Thapa", "sex": "M", "smoking": "No", "alcohol": "No" }
Current Medication : [ { "drug_name": "test drug", "drug_unit": "tab", "dosage": "2 wk", "initiation_at": "2021-05-20" }, { "drug_name": "test drug 2", "drug_unit": "ph", "dosage": "1 week", "initiation_at": "2021-05-20" } ]
Lab Investigation : [ { "name": "cbc", "value": "20", "unit": "umg", "date": "2021-05-20" }, { "name": "hdl", "value": "22", "unit": "umg", "date": "2021-05-21" } ]
Prescription : [ { "p_name": "Cetamol", "p_unit": "tab", "p_dosage": "2 wk", "duration": "10" } ]

On Controller I have done like this.

public function store(Request $request)
    {
        $TelePatient = TelePatient::create($request->patient);
        $TelMedis = $request->currMed;
        $preMeds = $request->preMed;
        $labInvs = $request->labInv;
        foreach ($TelMedis as $Telmidi) {
            $patId = ['tel_patient_id' => $TelePatient->id];
            $TelMd = array_merge($Telmidi,$patId);
            TeleMedication::create($TelMd);
            }
        foreach ($preMeds as $preMed) {
            $patId = ['tel_patient_id' => $TelePatient->id];
            $TelpreMed = array_merge($preMed,$patId);
            TelePrescribedMedication::create($TelpreMed);
            }
        foreach ($labInvs as $labInv) {
            $patId = ['tel_patient_id' => $TelePatient->id];
            $TelLab = array_merge($labInv,$patId);
            TeleLabInvestigation::create($TelLab);
            }        
        return response()->json('The Patient data successfully added');
    }

So if you have eloquent and better way. Please suggest.

Now I have to update tele_patient table, with rest 3 onetomany relationships tables. I am researching for it. If you have idea please share the eloquent best way for updating.

0 likes
3 replies
SilenceBringer's avatar
Level 55

@bhojkamal if you have correct relationships, you can do something like (replace medications() with your real relationship name):


        $TelePatient = TelePatient::create($request->patient);
        
        $TelePatient->medications()->createMany($request->currMed);
        // similar for other relations

        return response()->json('The Patient data successfully added');
    }
1 like
bhojkamal's avatar

@silencebringer Thanks. That's great. I found same on documentation too once posted this question. It worked and code became really short.

What about the edit/update? Can we do from one form. Or Do we need to separate form for update? e.g. to update the medications table with rest 3 tables? What would be the best approach for it? If I go separately for each, it is not big deal I can do it. However If possible, I want to do from one form like adding new patient details as above.

bhojkamal's avatar

I found solution for updating multiple data to multiple table using the 3rd party package - "mavinoo/laravel-batch".

The batch update.

1 like

Please or to participate in this conversation.