I have a script that saves user addresses during order process. If a customer want to add new address i'm showing a form and handle this process with ajax.
When saving address successful, i'm hiding the form.
Here is my code.
//Save Address
DB::beginTransaction();
try
{
$addressData['new_address_id'] = $this->insertAddress($addressData);
if($addressData['default_address'] == 1)
{
(new Address)->removeDefaultAddress(Auth::user()->id);
(new Address)->assignDefaultAddress(Auth::user()->id, $addressData['new_address_id']);
}
DB::commit();
if(!is_null($addressData['new_address_id']))
{
Session::put('order_delivery_address', $addressData['new_address_id']);
}
return response()->json(['success' => true, 'addressData' => $addressData], 200);
}
catch(\Exception $e)
{
DB::rollback();
throw $e;
Session::forget('order_delivery_address');
return response()->json([
'success' => 'false',
'errors' => trans('main.db_unknown_exception'),
], 422);
}
I think that i didn't understand db transactions. Because if there is a problem with transaction we are catch exception. But new address form is hiding. Because http respond 200 is returns.
I think i wrote it in wrong places.