brand, $request->email)->exists()) { throw new ApiErrorException(ApiError::singleOptinCustomerExists()); } /** @var \App\Models\SingleOptin */ $singleOptin = SingleOptin::where('email', $request->email) ->whereHas('brand', function ($q) use ($service) { $q->whereId($service->brand->id); })->firstOrNew(); if ($request->opt_in) { $singleOptin->fill($request->validated()); $singleOptin->brand()->associate($service->brand); $success = $singleOptin->save(); } else { $success = $singleOptin->delete() !== false; } if (!$success) { // @codeCoverageIgnoreStart throw new CrmException('Api failed to update single optin.'); // @codeCoverageIgnoreEnd } return response()->json(); } }