优化 masterSku 命名

This commit is contained in:
Edward Yang 2023-01-12 15:46:49 +08:00
parent 181faeb4a1
commit 100333d56f
7 changed files with 9 additions and 9 deletions

View File

@ -16,7 +16,7 @@ class ProductResource extends JsonResource
*/ */
public function toArray($request): array public function toArray($request): array
{ {
$masterSku = $this->master_sku; $masterSku = $this->masterSku;
$data = [ $data = [
'id' => $this->id, 'id' => $this->id,

View File

@ -51,7 +51,7 @@ class Product extends Base
return $this->hasMany(ProductAttribute::class); return $this->hasMany(ProductAttribute::class);
} }
public function master_sku() public function masterSku()
{ {
return $this->hasOne(ProductSku::class)->where('is_default', 1); return $this->hasOne(ProductSku::class)->where('is_default', 1);
} }

View File

@ -32,7 +32,7 @@ class ProductRepo
if (is_int($product)) { if (is_int($product)) {
$product = Product::query()->findOrFail($product); $product = Product::query()->findOrFail($product);
} }
$product->load('description', 'skus', 'master_sku', 'brand', 'relations'); $product->load('description', 'skus', 'masterSku', 'brand', 'relations');
return $product; return $product;
} }
@ -61,7 +61,7 @@ class ProductRepo
if (! $productIds) { if (! $productIds) {
return ProductSimple::collection(new Collection()); return ProductSimple::collection(new Collection());
} }
$builder = self::getBuilder(['product_ids' => $productIds])->whereHas('master_sku'); $builder = self::getBuilder(['product_ids' => $productIds])->whereHas('masterSku');
$products = $builder->with('inCurrentWishlist')->get(); $products = $builder->with('inCurrentWishlist')->get();
return ProductSimple::collection($products); return ProductSimple::collection($products);
@ -75,7 +75,7 @@ class ProductRepo
*/ */
public static function getBuilder(array $data = []): Builder public static function getBuilder(array $data = []): Builder
{ {
$builder = Product::query()->with('description', 'skus', 'master_sku', 'attributes'); $builder = Product::query()->with('description', 'skus', 'masterSku', 'attributes');
$builder->leftJoin('product_descriptions as pd', function ($build) { $builder->leftJoin('product_descriptions as pd', function ($build) {
$build->whereColumn('pd.product_id', 'products.id') $build->whereColumn('pd.product_id', 'products.id')

View File

@ -23,7 +23,7 @@ class BrandController extends Controller
$brand = BrandRepo::find($id); $brand = BrandRepo::find($id);
$products = $brand->products() $products = $brand->products()
->with([ ->with([
'master_sku', 'masterSku',
'description', 'description',
'inCurrentWishlist', 'inCurrentWishlist',
]) ])

View File

@ -21,7 +21,7 @@ class WishlistDetail extends JsonResource
public function toArray($request): array public function toArray($request): array
{ {
$product = $this->product; $product = $this->product;
$masterSku = $product->master_sku; $masterSku = $product->masterSku;
$image = $this->product->image ?: $masterSku->image; $image = $this->product->image ?: $masterSku->image;
$productName = $product->description->name ?? ''; $productName = $product->description->name ?? '';

View File

@ -25,7 +25,7 @@ class ProductSimple extends JsonResource
*/ */
public function toArray($request): array public function toArray($request): array
{ {
$masterSku = $this->master_sku; $masterSku = $this->masterSku;
if (empty($masterSku)) { if (empty($masterSku)) {
throw new \Exception("invalid master sku for product {$this->id}"); throw new \Exception("invalid master sku for product {$this->id}");
} }

View File

@ -29,7 +29,7 @@ class MenusController extends Controller
public function latestProducts() public function latestProducts()
{ {
$products = ProductRepo::getBuilder(['active' => 1]) $products = ProductRepo::getBuilder(['active' => 1])
->whereHas('master_sku') ->whereHas('masterSku')
->with('inCurrentWishlist') ->with('inCurrentWishlist')
->paginate(perPage()); ->paginate(perPage());