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 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 category slug (ProductCategories) */ public function show(Request $request, string $slug) { // Find category by slug $category = ProductCategories::with(['parent', 'children']) ->where('slug', $slug) ->where('is_active', 1) ->first(); if (!$category) { abort(404, 'Danh mục sản phẩm không tồn tại'); } // Get products with pagination and pricing $products = $category->products() ->with([ 'pricings' => function ($query) { $query->where('is_active', 1)->orderBy('price'); }, 'osVersions.osGroup', 'controlPanels' ]) ->where('is_active', 1) ->orderBy('created_at', 'desc') ->paginate(12); return view('frontend.service.index', [ 'category' => $category, 'products' => $products, 'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $category->name), ]); } /** * Get products by type and group */ private function getProductsByTypeAndGroup(int $typeId, int $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 */ private function getGroupsByType(int $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 }; } }