diff --git a/beike/Shop/Http/Controllers/ProductController.php b/beike/Shop/Http/Controllers/ProductController.php index d4452bf5..d04a7d42 100644 --- a/beike/Shop/Http/Controllers/ProductController.php +++ b/beike/Shop/Http/Controllers/ProductController.php @@ -16,7 +16,7 @@ class ProductController extends Controller $data = [ 'product' => (new ProductDetail($product))->jsonSerialize(), ]; - +dd($data); return view('product', $data); } } diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php index caa97164..e9abfd9e 100644 --- a/beike/Shop/Http/Resources/ProductDetail.php +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -23,8 +23,24 @@ class ProductDetail extends JsonResource 'description' => $this->description->description ?? '', 'image' => image_resize($this->image), 'category_id' => $this->category_id ?? null, - 'variables' => $this->variables, + 'variables' => $this->decodeVariables($this->variables), 'skus' => SkuDetail::collection($this->skus)->jsonSerialize(), ]; } + + private function decodeVariables($variables) + { + $lang = current_language_code(); + return array_map(function ($item) use ($lang){ + return [ + 'name' => $item['name'][$lang], + 'values' => array_map(function ($item) use ($lang){ + return [ + 'name' => $item['name'][$lang], + 'image' => image_resize($item['image'], 100, 100), + ]; + }, $item['values']), + ]; + }, $variables); + } }