where('slug', $slug) ->withCount('media','approvedReviews') ->withAvg('approvedReviews', 'rating') ->active() ->hasQuantity() ->firstOrFail(); $relatedProducts = Product::with('firstMedia')->whereHas('category', function ($query) use ($product) { $query->whereId($product->category_id); }) ->where('id', '<>', $product->id) ->inRandomOrder() ->active() ->hasQuantity() ->take(4) ->get(['id', 'slug', 'name', 'price']); return view('frontend.product.show', compact('product', 'relatedProducts')); } }