Skip to content

Commit 3ca802f

Browse files
committed
chore: refactoring product controller
1 parent 45f20c7 commit 3ca802f

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

app/Http/Controllers/Frontend/ProductController.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,44 @@ public function show($slug)
3939
{
4040
$product = Product::where('slug', $slug)->with([
4141
'category',
42+
'variants.attributeValues'
4243
])->firstOrFail();
43-
$variantIds = ProductVariant::where('product_id', $product->id)->pluck('id');
44+
$product->image = $this->getImageUrl($product->image);
45+
// $variantIds = ProductVariant::where('product_id', $product->id)->pluck('id');
4446

45-
$attributeValueIds = DB::table('product_variant_values')
46-
->whereIn('product_variant_id', $variantIds)
47-
->pluck('attribute_value_id')
48-
->unique();
47+
// $attributeValueIds = DB::table('product_variant_values')
48+
// ->whereIn('product_variant_id', $variantIds)
49+
// ->pluck('attribute_value_id')
50+
// ->unique();
4951

50-
$attributeIds = AttributeValue::whereIn('id', $attributeValueIds)
51-
->pluck('attribute_id')
52-
->unique();
52+
// $attributeIds = AttributeValue::whereIn('id', $attributeValueIds)
53+
// ->pluck('attribute_id')
54+
// ->unique();
5355

54-
$attributes = Attribute::with(['values' => function ($query) use ($attributeValueIds) {
55-
$query->whereIn('id', $attributeValueIds);
56-
}])
57-
->whereIn('id', $attributeIds)
58-
->get();
56+
// $attributes = Attribute::with(['values' => function ($query) use ($attributeValueIds) {
57+
// $query->whereIn('id', $attributeValueIds);
58+
// }])
59+
// ->whereIn('id', $attributeIds)
60+
// ->get();
5961

60-
$product->load('variants.attributeValues');
62+
// $product->load('variants.attributeValues');
63+
64+
$attributes = Attribute::whereHas('values.variants', function($q) use ($product) {
65+
$q->where('product_id', $product->id);
66+
})->with(['values' => function($q) use ($product) {
67+
$q->whereHas('variants', function($q2) use ($product) {
68+
$q2->where('product_id', $product->id);
69+
});
70+
}])->get();
6171

62-
// For the main product image
63-
$product->image = $product->image ? (Str::startsWith($product->image, ['http://', 'https://']) ? $product->image : Storage::disk('public')->url($product->image)) : '';
6472

6573
// For the variant images
6674
$product->variants->transform(function ($v) {
6775
return [
6876
'id' => $v->id,
6977
'price' => $v->price,
7078
'sku' => $v->sku,
71-
'image' => $v->image ? (Str::startsWith($v->image, ['http://', 'https://']) ? $v->image : Storage::disk('public')->url($v->image)) : '',
79+
'image' => $this->getImageUrl($v->image),
7280
'attribute_value_ids' => $v->attributeValues->pluck('id')->sort()->values()->all(),
7381
];
7482
});
@@ -81,4 +89,8 @@ public function pcBuilder(){
8189
$products = Product::whereIn('category_id', $categories->pluck('id'))->get();
8290
return view('frontend.pc-builder.index', compact('categories','products'));
8391
}
92+
93+
private function getImageUrl($image){
94+
return $image ? ((Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::disk('public')->url($image))) : '';
95+
}
8496
}

0 commit comments

Comments
 (0)