function ($query) { $query->where('status', 1); // Only active groups }]) ->whereNotIn('id', [1, 2]) ->where('status', 1) // Only active types ->latest('id') ->get(); }); // Handle product filtering with optimized queries if ($request->type && $request->group) { $product = $this->getProductsByTypeAndGroup($request->type, $request->group); $group = $this->getGroupsByType($request->type); return view('frontend.shop', compact('title', 'type', 'group', 'product')); } if ($request->type) { $group = $this->getGroupsByType($request->type); return view('frontend.shop', compact('title', 'type', 'group')); } return view('frontend.shop', compact('title', 'type')); } /** * Add product to cart with validation */ public function store(AddToCartRequest $request, Cart $cart) { $product = $this->findProduct($request->type_id, $request->product_id); if (!$product) { return back()->with('error', 'Sản phẩm không tồn tại!'); } $cart->addToCart($request->user('users')->id, $request->product_price, $product); return redirect()->back()->with('success', 'Thêm vào giỏ hàng thành công!'); } /** * Display products by group slug with pagination and optimization */ public function show(Request $request, string $slug) { // Cache group data for better performance $page = (int) $request->get('page', 1); $cacheKey = "group_products_{$slug}_page_{$page}"; $data = Cache::remember($cacheKey, 1800, function () use ($slug, $page) { // Find group with optimized query $group = Group::with(['type']) ->where('slug', $slug) ->where('status', 1) // Only active groups ->first(); if (!$group) { return null; } // Get products with pagination based on group type $products = $this->getPaginatedProducts($group, $page); return [ 'group' => $group, 'products' => $products, ]; }); if (!$data) { abort(404, 'Nhóm sản phẩm không tồn tại'); } return view('frontend.service.index', [ 'group' => $data['group'], 'products' => $data['products'], 'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $data['group']->name), ]); } /** * Get products by type and group with optimization */ private function getProductsByTypeAndGroup(int $typeId, int $groupId) { return Cache::remember("products_type_{$typeId}_group_{$groupId}", 1800, function () use ($typeId, $groupId) { return match ($typeId) { 3 => Hosting::where('group_id', $groupId) ->where('status', 1) ->with(['group', 'type']) ->get(), 4 => VPS::where('group_id', $groupId) ->where('status', 1) ->with(['group', 'type', 'operatingSystems.osGroup']) ->get(), default => collect() }; }); } /** * Get groups by type with optimization */ private function getGroupsByType(int $typeId) { return Cache::remember("groups_type_{$typeId}", 3600, function () use ($typeId) { return Group::with('type') ->where('type_id', $typeId) ->where('status', 1) ->get(); }); } /** * Find product by type and ID */ private function findProduct(int $typeId, int $productId) { return match ($typeId) { 3 => Hosting::where('id', $productId) ->where('status', 1) ->first(), 4 => VPS::where('id', $productId) ->where('status', 1) ->first(), default => null }; } /** * Get paginated products based on group type */ private function getPaginatedProducts(Group $group, int $page = 1, int $perPage = 12) { $query = null; // Determine which model to query based on group type switch ($group->type_id) { case 3: // Hosting $query = Hosting::where('group_id', $group->id) ->where('status', 1) ->with(['group', 'type']) ->orderBy('created_at', 'desc'); break; case 4: // VPS $query = VPS::where('group_id', $group->id) ->where('status', 1) ->with(['group', 'type']) ->orderBy('created_at', 'desc'); break; default: return collect()->paginate($perPage); } return $query->paginate($perPage, ['*'], 'page', $page); } }