diff --git a/app/Services/PriceBroadcastService.php b/app/Services/PriceBroadcastService.php index 0c25546..5820e09 100644 --- a/app/Services/PriceBroadcastService.php +++ b/app/Services/PriceBroadcastService.php @@ -171,11 +171,21 @@ class PriceBroadcastService $identity = []; foreach ($listPayload as $product) { + if ($product instanceof \Illuminate\Database\Eloquent\Model) { + $product = $product->toArray(); + } + if (! is_array($product)) { continue; } - $file = is_array($product['file'] ?? null) ? $product['file'] : []; + $file = $product['file'] ?? null; + if ($file instanceof \Illuminate\Database\Eloquent\Model) { + $file = $file->toArray(); + } + if (! is_array($file)) { + $file = []; + } $identity[] = [ 'id' => (int) ($product['id'] ?? 0), @@ -218,7 +228,9 @@ class PriceBroadcastService private function buildProductListPayload(): ?array { if (app(ConfigService::class)->isStore()) { - return app(ProductController::class)->customerStoreProductListPayload(); + return $this->normalizeListPayload( + app(ProductController::class)->customerStoreProductListPayload() + ); } $request = Request::create('/customer/product/list', 'GET'); @@ -229,4 +241,31 @@ class PriceBroadcastService return is_array($decoded) ? $decoded : null; } + + /** + * مدل/کالکشن Eloquent را به آرایهٔ خالص تبدیل می‌کند تا listIdentity و SSE + * همیشه file_id و URL تصویر را ببینند (قبلاً is_array روی Model شکست می‌خورد). + * + * @param mixed $payload + * @return array|null + */ + private function normalizeListPayload(mixed $payload): ?array + { + if ($payload === null) { + return null; + } + + if ($payload === []) { + return []; + } + + $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); + if ($encoded === false) { + return null; + } + + $decoded = json_decode($encoded, true); + + return is_array($decoded) ? $decoded : null; + } }