You could use ajax as l to load from the external api and then insert it but then you risk those interuptions.
I would most likely use a queue worker for that. That way it will be done in the background instead of risking interuptions by the user.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is there a way to get a response from API request and insert it to the database asynchronous?
public function store(Doctors $doctors, Request $request) {
$doctors = Doctors::create(array_merge(request([
'doctor_email',
'doctor_telephone',
'totalCost',
'case_number'
]),
['user_id' => auth()->id(),
'totalCost' => $GLOBALS['totalCost'],
// 'case_number' => $GLOBALS['caseNumber'], Fix this problem
]));
// This is where I send the request to Api
$this->AddCaseWebPortal ($request, $doctorsChrome);
}
// The API request
public function AddCaseWebPortal (Request $request, Doctors $doctors){
$clienttest2 = new SoapClient('***');
$paramstest2 = array(
'addCaseReq' => array(
'Case' => array
'ShipDate' => date("Y-m-d"),
'DateIn'=> date("Y-m-d"),
'Address'=> $address,
'CustomerID' => $customerId,
'PatientFirst' => $request->patient_firstname,
try {
$responsetest2 = $clienttest2->__soapCall("AddCase", array($paramstest2));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\r\n";
}
}
// I get the response
// dd($responsetest2->AddCaseResult->CaseNumber);
// $GLOBALS['caseNumber'] = $responsetest2->AddCaseResult->CaseNumber;
Please or to participate in this conversation.