From 464b2e5d77771fa808e232e5f5ac5e1ce8441db5 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 27 Jun 2022 20:18:59 +0800 Subject: [PATCH] fixed product sku --- beike/Models/Product.php | 5 +++++ beike/Shop/Http/Controllers/ProductController.php | 3 ++- beike/Shop/Repositories/ProductRepo.php | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/beike/Models/Product.php b/beike/Models/Product.php index 09f58416..4261516e 100644 --- a/beike/Models/Product.php +++ b/beike/Models/Product.php @@ -39,6 +39,11 @@ class Product extends Model return $this->hasMany(ProductSku::class); } + public function master_sku() + { + return $this->hasOne(ProductSku::Class)->where('is_default', 1); + } + public function getPriceFormattedAttribute(): string { return '$' . $this->price; diff --git a/beike/Shop/Http/Controllers/ProductController.php b/beike/Shop/Http/Controllers/ProductController.php index a0397d84..839846e1 100644 --- a/beike/Shop/Http/Controllers/ProductController.php +++ b/beike/Shop/Http/Controllers/ProductController.php @@ -3,13 +3,14 @@ namespace Beike\Shop\Http\Controllers; use Beike\Models\Product; +use Beike\Shop\Repositories\ProductRepo; use Illuminate\Http\Request; class ProductController extends Controller { public function show(Request $request, Product $product) { - $product->load('description', 'skus'); + $product = ProductRepo::getProductDetail($product); $data = [ 'product' => $product, diff --git a/beike/Shop/Repositories/ProductRepo.php b/beike/Shop/Repositories/ProductRepo.php index 21f25541..2f8d1251 100644 --- a/beike/Shop/Repositories/ProductRepo.php +++ b/beike/Shop/Repositories/ProductRepo.php @@ -17,6 +17,20 @@ use Illuminate\Database\Eloquent\Builder; class ProductRepo { + /** + * 获取产品详情 + */ + public static function getProductDetail($product) + { + if (is_int($product)) { + $product = Product::query()->findOrFail($product); + } + $product->load('description', 'skus', 'master_sku'); + dd($product); + return $product; + } + + /** * 通过多个产品分类获取产品列表 *