diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php new file mode 100644 index 0000000..32f00f8 --- /dev/null +++ b/app/Http/Controllers/SearchController.php @@ -0,0 +1,95 @@ +get('q'); + + if (empty($query) || strlen($query) < 2) { + return response()->json([ + 'success' => true, + 'results' => [], + 'message' => 'Please enter at least 2 characters' + ]); + } + + // Search products + $products = Items::where('is_publish', true) + ->where('deleted_at', null) + ->where(function($q) use ($query) { + $q->where('name', 'ILIKE', "%{$query}%") + ->orWhere('number', 'ILIKE', "%{$query}%") + ->orWhere('description', 'ILIKE', "%{$query}%"); + }) + ->select('id', 'name', 'number', 'slug') + ->limit(8) + ->get(); + + // Search categories + $categories = StoreCategory::where('name', 'ILIKE', "%{$query}%") + ->select('id', 'name') + ->limit(3) + ->get(); + + // Search genders + $genders = Gender::where('name', 'ILIKE', "%{$query}%") + ->select('id', 'name') + ->limit(3) + ->get(); + + // Format results + $results = []; + + if ($products->isNotEmpty()) { + $results['products'] = $products->map(function($product) { + $price = $product->display_price; + return [ + 'id' => $product->id, + 'name' => $product->name, + 'number' => $product->number, + 'slug' => $product->slug ?? $product->id, + 'type' => 'product', + 'route' => route('product.detail', $product->slug) + ]; + }); + } + + if ($categories->isNotEmpty()) { + $results['categories'] = $categories->map(function($category) { + return [ + 'id' => $category->id, + 'name' => $category->name, + 'slug' => $category->slug, + 'type' => 'category', + 'route' => route('product.index',['filter[category]'=>$category->id]) + ]; + }); + } + + if ($genders->isNotEmpty()) { + $results['genders'] = $genders->map(function($gender) { + return [ + 'id' => $gender->id, + 'name' => $gender->name, + 'slug' => $gender->slug, + 'type' => 'gender', + 'route' => route('product.index',['filter[gender]'=>$gender->id]) + ]; + }); + } + + return response()->json([ + 'success' => true, + 'results' => $results, + 'query' => $query + ]); + } +} diff --git a/app/Repositories/Catalog/ProductRepository.php b/app/Repositories/Catalog/ProductRepository.php index 0c323cc..43f8dd9 100644 --- a/app/Repositories/Catalog/ProductRepository.php +++ b/app/Repositories/Catalog/ProductRepository.php @@ -39,21 +39,29 @@ class ProductRepository $where_ids = []; if ($price_range_start && $price_range_end) { - $params_filter = []; - $params_filter['location_id'] = $location_id; - $params_filter['price_range_start'] = $price_range_start; - $params_filter['price_range_end'] = $price_range_end; + $params_filter = [ + 'location_id' => $location_id, + 'price_range_start' => $price_range_start, + 'price_range_end' => $price_range_end, + ]; - $where_ids = collect($this->getProductList($params_filter))->pluck('id')->toArray(); + $where_ids = collect($this->getProductList($params_filter)) + ->pluck('id') + ->unique() + ->values() + ->toArray(); - if (! empty($sorting_ids) && count($sorting_ids) > 0) { - $where_ids = array_values( - array_intersect($sorting_ids, $where_ids) - ); + // Jika sudah ada sorting_ids → ikuti urutannya + if (!empty($sorting_ids)) { + $sorting_ids = array_values(array_intersect($sorting_ids, $where_ids)); + } else { + // kalau belum ada sorting, pakai hasil filter + $sorting_ids = $where_ids; } - } + + $builder = Items::select('items.*', 'percent') ->leftJoin('item_dimension', 'item_dimension.no', 'items.number') ->leftJoin(DB::raw("(select distinct on (item_id) item_id, percent, discounts.created_at, @@ -73,20 +81,18 @@ class ProductRepository $query->orderBy('percent', 'desc'); } else { - if (! empty($sorting_ids) && count($sorting_ids) > 0) { - - $caseStatements = []; - foreach ($sorting_ids as $index => $id) { - $caseStatements[] = "WHEN items.id = {$id} THEN " . ($index + 1); - } - $caseStatements[] = "ELSE 999"; - - $query->orderByRaw(" - CASE - " . implode("\n ", $caseStatements) . " - END ASC - "); + if (!empty($sorting_ids)) { + $cases = collect($sorting_ids)->map(function ($id, $index) { + return "WHEN items.id = {$id} THEN {$index}"; + })->implode(' '); + + $query->orderByRaw(" + CASE + {$cases} + ELSE 999999 + END + "); } elseif ($sort == 'new') { $query->orderByRaw("case when category1 in ('CLUBS','CLUB','COMPONENT HEAD') and brand = 'PXG' then 1 else 2 end ASC"); } else { @@ -113,8 +119,8 @@ class ProductRepository ->when($event, function ($query) use ($event) { $query->where('d.event', $event); }) - ->when($where_ids, function ($query) use ($where_ids) { - $query->whereIn('items.id', $where_ids); + ->when($sorting_ids, function ($query) use ($sorting_ids) { + $query->whereIn('items.id', $sorting_ids); }); if ($category_id) { @@ -159,6 +165,7 @@ class ProductRepository $order_by = 'ORDER BY items.created_at DESC NULLS LAST'; } + $location_id = $params['location_id'] ?? null; // ========================= @@ -210,13 +217,13 @@ class ProductRepository AND (d.valid_at IS NULL OR d.valid_at <= NOW()) AND (d.expired_at IS NULL OR d.expired_at >= NOW()) AND ( - (22 IS NOT NULL AND (d.location_id = 22 OR d.location_id IS NULL)) - OR (22 IS NULL AND d.location_id IS NULL) + ($location_id IS NOT NULL AND (d.location_id = $location_id OR d.location_id IS NULL)) + OR ($location_id IS NULL AND d.location_id IS NULL) ) ORDER BY di.item_reference_id, CASE - WHEN d.location_id = 22 THEN 1 + WHEN d.location_id = $location_id THEN 1 ELSE 2 END, d.created_at DESC @@ -237,13 +244,13 @@ class ProductRepository AND (d.valid_at IS NULL OR d.valid_at <= NOW()) AND (d.expired_at IS NULL OR d.expired_at >= NOW()) AND ( - (22 IS NOT NULL AND (d.location_id = 22 OR d.location_id IS NULL)) - OR (22 IS NULL AND d.location_id IS NULL) + ($location_id IS NOT NULL AND (d.location_id = $location_id OR d.location_id IS NULL)) + OR ($location_id IS NULL AND d.location_id IS NULL) ) ORDER BY di.item_reference_id, CASE - WHEN d.location_id = 22 THEN 1 + WHEN d.location_id = $location_id THEN 1 ELSE 2 END, d.created_at DESC diff --git a/resources/views/components/layout/header.blade.php b/resources/views/components/layout/header.blade.php index 3747604..62d49cb 100644 --- a/resources/views/components/layout/header.blade.php +++ b/resources/views/components/layout/header.blade.php @@ -130,25 +130,18 @@ + {{-- search box --}}
Start typing in the search field above to see instant search results.
-