public function import_data(Request $request) { $success = true; try { DB::beginTransaction(); foreach ($request->array_or_object as $item) { $record_payload = [ "name" => $item->name, "surname" => $item->surname, ]; $record_id = $item->id ?? null; $inserted_record = RecordModel::updateOrCreate(['id' => $record_id], $record_payload); if (!$inserted_record) { $success = false; break; // Stop further processing if any insert fails } } if ($success) { DB::commit(); return ['status' => true, 'message' => 'Success, Your import has completed with no issues.']; } else { DB::rollBack(); return ['status' => false, 'message' => 'Unsuccessful Transaction. Not an exception.']; } } catch (\Exception $e) { DB::rollBack(); $i = count($e->errorInfo) - 1; return ['status' => false, 'message' => $e->errorInfo[$i]]; } }
Add to favorites