validate([ 'email' => ['nullable', 'email', 'max:255'], 'mobile' => ['nullable', 'string', 'max:30'], ]); $email = isset($validated['email']) ? trim($validated['email']) : null; $mobile = isset($validated['mobile']) ? trim($validated['mobile']) : null; if ($email === null || $email === '') { $email = null; } if ($mobile === null || $mobile === '') { $mobile = null; } if ($email === null && $mobile === null) { return response()->json([ 'message' => 'حداقل ایمیل یا شماره موبایل را وارد کنید.', 'errors' => [ 'email' => ['حداقل ایمیل یا شماره موبایل را وارد کنید.'], ], ], 422); } if ($email !== null) { $existing = NewsletterSubscription::where('email', $email)->first(); if ($existing !== null) { return response()->json([ 'message' => 'شما قبلاً در خبرنامه عضویت داشتهاید.', 'subscribed' => true, ]); } } $subscription = NewsletterSubscription::create([ 'email' => $email, 'mobile' => $mobile, ]); return response()->json([ 'message' => 'عضویت شما در خبرنامه با موفقیت ثبت شد.', 'subscription' => $subscription, ], 201); } }