From 80f6dc86125cedbfa876b1b701f71238a5753430 Mon Sep 17 00:00:00 2001 From: Bayu Lukman Yusuf Date: Wed, 7 Jan 2026 14:01:43 +0700 Subject: [PATCH] load product ajax --- app/Http/Controllers/ProductController.php | 93 ++++- .../views/shop/catalog-fashion.blade.php | 321 +++++++++++++++--- routes/web.php | 3 + 3 files changed, 351 insertions(+), 66 deletions(-) diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 5bf24aa..8256de0 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -12,27 +12,66 @@ use Illuminate\Support\Facades\Cache; class ProductController extends Controller { - public function index(Request $request) + public function genders(Request $request) + { + $genderRepository = new GenderRepository; + $genders = $genderRepository->getList([]); + + // Render gender links HTML + $genderHtml = ''; + $currentGenderId = $request->input('current_gender'); + + foreach ($genders as $gender) { + $isActive = $currentGenderId == $gender->id; + $genderHtml .= ''; + } + + return response()->json([ + 'success' => true, + 'genders' => $genderHtml + ]); + } + + public function categories(Request $request) + { + $categoryRepository = new CategoryRepository; + $categories = $categoryRepository->getList([]); + + // Render category links HTML + $categoryHtml = ''; + $currentCategoryId = $request->input('current_category'); + + foreach ($categories as $category) { + $isActive = $currentCategoryId == $category->id; + $categoryHtml .= ''; + } + + return response()->json([ + 'success' => true, + 'categories' => $categoryHtml + ]); + } + + public function ajax(Request $request) { $limit = 20; $page = $request->page ?? 1; $search = $request->search; - $filter = $request->filter ?? []; $sortBy = $request->sort_by ?? 'relevance'; $price_range_start = $request->price_range_start ?? null; $price_range_end = $request->price_range_end ?? null; - $genderRepository = new GenderRepository; - $categoryRepository = new CategoryRepository; - - $genders = $genderRepository->getList([]); - $categories = $categoryRepository->getList([]); - - - $user = auth()->user(); $userId = $user ? $user->id : 0; [$location_id, $is_consignment] = Cache::remember('employee_user_'.$userId, 60 * 60 * 24, function () use ($user) { @@ -63,7 +102,17 @@ class ProductController extends Controller 'price_range_end' => $price_range_end, ]); + // Render product cards HTML + $productHtml = ''; + foreach ($products as $product) { + $productHtml .= '
'; + $productHtml .= view('components.home.product-card', ['product' => $product])->render(); + $productHtml .= '
'; + } + + // filter + $filter = $request->filter ?? []; if (isset($filter['category']) && $filter['category']){ $category = StoreCategory::find($filter['category']); @@ -87,16 +136,32 @@ class ProductController extends Controller $filters = $filter; + return response()->json([ + 'success' => true, + 'filters' => $filters, + 'products' => $productHtml, + 'count' => count($products), + 'has_more' => count($products) >= $limit + ]); + } + public function index(Request $request) + { + + + $productRepository = new ProductRepository; + $products = []; + + + + $filters = []; + $min_max_price = $productRepository->getMinMaxPrice(); return view('shop.catalog-fashion', [ - 'filters' => $filters, - 'genders' => $genders, - 'categories' => $categories, + 'products' => $products, - 'page' => $page, 'min_max_price' => $min_max_price, ]); } diff --git a/resources/views/shop/catalog-fashion.blade.php b/resources/views/shop/catalog-fashion.blade.php index c1d7621..9710fa7 100644 --- a/resources/views/shop/catalog-fashion.blade.php +++ b/resources/views/shop/catalog-fashion.blade.php @@ -33,28 +33,8 @@ data-bs-target="#filterSidebar" aria-label="Close"> -
- @if (count($filters) > 0) - -
-
-

Filter

- - Clear all - -
-
- @foreach ($filters as $key => $filter) - - - {{ $filter }} - - @endforeach - -
-
- @endif +
+
@@ -71,14 +51,8 @@ aria-labelledby="headingGenders">
-
@@ -97,14 +71,8 @@ aria-labelledby="headingCategories">
-
@@ -471,7 +439,7 @@
-
Found {{ count($products) }} +
Found Loading... items
@@ -495,18 +463,9 @@
-
- - @foreach ($products as $key => $value) - -
- -
- @endforeach - - - - +
+ +
{{--
@@ -551,7 +510,7 @@
- + Show more @@ -635,6 +594,264 @@ Filters + + @endsection @section('scripts') diff --git a/routes/web.php b/routes/web.php index fffb901..021305a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,4 +24,7 @@ Route::post('/locale/switch', [LocaleController::class, 'switch'])->name('locale Route::get('/', [HomeController::class, 'index'])->name('home'); Route::get('/products',[ProductController::class, 'index'])->name('product.index'); +Route::get('/products/ajax',[ProductController::class, 'ajax'])->name('product.ajax'); +Route::get('/products/ajax/categories',[ProductController::class, 'categories'])->name('product.ajax.categories'); +Route::get('/products/ajax/genders',[ProductController::class, 'genders'])->name('product.ajax.genders'); Route::get('/product/{slug}',[ProductController::class, 'detail'])->name('product.detail'); \ No newline at end of file