middleware(SetProductSortOption::class)->only('index'); } /** * Display a listing of the resource. * * @param Product $model * @param ProductFilter $productFilter * * @return JsonResponse|Application|Factory|View */ public function index(Product $model, ProductFilter $productFilter) { if (request()->expectsJson()) { return $this->searchProducts($model, $productFilter); } return view('public.products.index'); } /** * Show the specified resource. * * @param string $slug * * @return Response */ public function show($slug) { $product = ProductRepository::findBySlug($slug); $relatedProducts = $product->relatedProducts()->forCard()->get(); $upSellProducts = $product->upSellProducts()->forCard()->get(); $review = $this->getReviewData($product); $product->append([ 'is_in_flash_sale', 'flash_sale_end_date', 'formatted_price_range', ]); event(new ProductViewed($product)); return view('public.products.show', compact('product', 'relatedProducts', 'upSellProducts', 'review')); } private function getReviewData(Product $product) { if (!setting('reviews_enabled')) { return null; } return Review::countAndAvgRating($product); } }