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

monyancha's avatar

How to store API Json Response to db In Laravel Controller

How to store API Json Response to db In Laravel Controller. Here is the controller >>

public function diagnosisStore(Request $request)
{
        $request->validate([
            'symptoms' => 'required',
            'gender' => 'required|string|max:50',
            'dob' => 'required|min:4',
        ]);
    
    
    $user = Auth::user();
    
    $tokenGenerator = new Token($this->config['username'],$this->config['password'],$this->config['authServiceUrl']);
    $token = $tokenGenerator->loadToken();
    
    if (!isset($token))
        exit();
        
    $api_medic = new Apimedic($token, $this->config['healthServiceUrl'], 'en-gb');
    
    $symptoms = $request['symptoms'];

    $input = $request->all();
    
    $selectedSymptoms = $input['symptoms'];
    $gender = $input['gender'];
    $dob = $input['dob'];

    $input['diagnosis'] = $api_medic->loadDiagnosis($selectedSymptoms, $gender, $dob);

    Diagnose::create($input);

    $notify[] = ['success', 'Diagnosis initiated Successfully.'];
    return back()->withNotify($notify);
}

And here is the JSON Response from the API

^ array:3 [▼ 0 => array:2 [▼ "Issue" => array:7 [▼ "ID" => 86 "Name" => "Coronary heart disease" "Accuracy" => 90 "Icd" => "I20;I21;I22;I23;I24;I25" "IcdName" => "Angina pectoris;Acute myocardial infarction;Subsequent myocardial infarction;Certain current complications following acute myocardial infarction;Other acute isc ▶" "ProfName" => "Coronary artery disease" "Ranking" => 1 ] "Specialisation" => array:3 [▼ 0 => array:3 [▼ "ID" => 1 "Name" => "Cardiology" "SpecialistID" => 0 ] 1 => array:3 [▼ "ID" => 15 "Name" => "General practice" "SpecialistID" => 0 ] 2 => array:3 [▼ "ID" => 19 "Name" => "Internal medicine" "SpecialistID" => 0 ] ] ] 1 => array:2 [▼ "Issue" => array:7 [▼ "ID" => 87 "Name" => "Heart attack" "Accuracy" => 67.49999 "Icd" => "I21;I22" "IcdName" => "Acute myocardial infarction;Subsequent myocardial infarction" "ProfName" => "Myocardial infarction" "Ranking" => 2 ] "Specialisation" => array:3 [▼ 0 => array:3 [▼ "ID" => 1 "Name" => "Cardiology" "SpecialistID" => 0 ] 1 => array:3 [▼ "ID" => 15 "Name" => "General practice" "SpecialistID" => 0 ] 2 => array:3 [▼ "ID" => 19 "Name" => "Internal medicine" "SpecialistID" => 0 ] ] ] 2 => array:2 [▼ "Issue" => array:7 [▼ "ID" => 167 "Name" => "Obstruction of a pulmonary artery" "Accuracy" => 28.125 "Icd" => "I26" "IcdName" => "Pulmonary embolism" "ProfName" => "Pulmonary embolism" "Ranking" => 3 ] "Specialisation" => array:3 [▼ 0 => array:3 [▼ "ID" => 15 "Name" => "General practice" "SpecialistID" => 0 ] 1 => array:3 [▼ "ID" => 19 "Name" => "Internal medicine" "SpecialistID" => 0 ] 2 => array:3 [▼ "ID" => 35 "Name" => "Pulmonology" "SpecialistID" => 0 ] ] ] ]

0 likes
3 replies
vincent15000's avatar

In your JSON data, you noticeably have several lines you want to add in your database.

When your controller retrieves these JSON datas, I think the best way is to handle them to obtain a list of objects you can then save in the database with this method.

https://laravel.com/docs/9.x/eloquent#inserts

monyancha's avatar

@vincent15000 I just want to save the response as it is then later i can retrieve it to the user dashboard

1 like
vincent15000's avatar

@monyancha If you want to store it as it is, why don't you store it in a JSON field ? But according to the datas, it's not a good idea to store this string as it is.

I suggest you to learn about MySQL.

Please or to participate in this conversation.