From 4ed20f7acb0e7b61f6fcbcb8bae7ca9d448545b0 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 25 Jul 2022 10:07:14 +0800 Subject: [PATCH] fixed variables --- beike/Shop/Http/Resources/ProductDetail.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php index 5a1fb9e2..aefe47ff 100644 --- a/beike/Shop/Http/Resources/ProductDetail.php +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -15,6 +15,9 @@ use Illuminate\Http\Resources\Json\JsonResource; class ProductDetail extends JsonResource { + /** + * @throws \Exception + */ public function toArray($request): array { return [ @@ -28,7 +31,15 @@ class ProductDetail extends JsonResource ]; } - private function decodeVariables($variables) + + /** + * 处理多规格商品数据 + * + * @param $variables + * @return array|array[] + * @throws \Exception + */ + private function decodeVariables($variables): array { $lang = current_language_code(); if (empty($variables)) { @@ -36,11 +47,11 @@ class ProductDetail extends JsonResource } return array_map(function ($item) use ($lang) { return [ - 'name' => $item['name'][$lang], + 'name' => $item['name'][$lang] ?? '', 'values' => array_map(function ($item) use ($lang) { return [ - 'name' => $item['name'][$lang], - 'image' => image_resize('catalog/'.$item['image'], 100, 100), + 'name' => $item['name'][$lang] ?? '', + 'image' => image_resize('catalog/' . $item['image']), ]; }, $item['values']), ];