where("id", $id)->firstOrFail(); $goods->view_total++; $goods->save(); return [ "id" => $goods->id, "name" => $goods->name, "category_name" => $goods->map->map(function (GoodsCategoryMap $map) { return optional($map->category)->name; })->implode("/"), "image_list" => $goods->image_list, "spec" => array_map(function ($specId) { try { /** @var Spec $spec */ $spec = Spec::find($specId); return [ "id" => $spec->id, "name" => $spec->name, "is_custom" => $spec->is_custom, "attr" => $spec->attrs->map(function (SpecAttr $a) { return [ "id" => $a->id, "name" => $a->name, ]; }), ]; } catch (\Exception $e) { throw new ClientException("商品规格有误,请联系管理员"); } }, $goods->spec), "sku" => $goods->sku->map(function (GoodsSku $sku) { if (empty($sku->sn)) { return null; } return [ "id" => $sku->id, "sn" => $sku->sn, "url_3d" => $sku->url_3d, "spec_attr" => $sku->spec_attr_list, ]; })->filter()->values(), "desc_html" => $goods->desc_html, "faq_html" => $goods->faq_html, "spec_attr_html" => $goods->spec_attr_html, "is_fav" => (int)$this->goodsIsFav($goods->id), ]; } protected function goodsIsFav($gid): bool { $ret = false; $uid = \Auth::id(); if ($uid) { if (UserFav::where("user_id", $uid)->whereGoodsId($gid)->first()) { $ret = true; } } return $ret; } }