diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index ed77bb9..e9b96ad 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use DB; class HomeController extends Controller { @@ -16,6 +17,13 @@ class HomeController extends Controller public function store(Request $request) { - return view('home.grocery'); + + + DB::listen(function ($query) { + logger($query->sql); + logger($query->bindings); + logger($query->time); + }); + return view('home.grocery'); } } diff --git a/app/Models/Items.php b/app/Models/Items.php index 18b1be4..2a8615e 100644 --- a/app/Models/Items.php +++ b/app/Models/Items.php @@ -89,7 +89,8 @@ class Items extends Model return $this->hasOne(ItemReference::class, 'item_id') ->where(function ($query) { $query->whereNull('item_variant_id')->orWhere('item_variant_id', 0); - }); + }) +->with(['item','itemVariant']); } public function variants() @@ -326,7 +327,8 @@ class Items extends Model public function getDisplayPriceAttribute() { - try { +return $this->discount_price * $this->conversion; +/* try { $convertion = $this->conversion_value(); $price = @$this->variants->first()->reference->price->price ?? null; @@ -336,7 +338,7 @@ class Items extends Model } catch (\Exception $e) { return 0; } - } +*/ } public function getDisplayPriceCurrencyAttribute() { diff --git a/app/Repositories/Catalog/BrandRepository.php b/app/Repositories/Catalog/BrandRepository.php index 320805d..8fd2b0e 100644 --- a/app/Repositories/Catalog/BrandRepository.php +++ b/app/Repositories/Catalog/BrandRepository.php @@ -13,6 +13,7 @@ class BrandRepository return Cache::remember('catalog_brand_'.json_encode($params), 60, function () use ($params) { $category_id = @$params['category_id']; + $limit = @$params['limit']; if ($category_id) { $ids = DB::select('select distinct brand from store_category_map a left join item_dimension b @@ -28,12 +29,15 @@ class BrandRepository return Brand::whereIn('name', $ids)->orderBy('priority', 'desc') ->where('priority', '>', 0) ->orderBy('name', 'asc') - + ->take(10) + ->withCount('items') ->get(); } return Brand::orderBy('priority', 'desc')->orderBy('name', 'asc') ->where('priority', '>', 0) + ->take(10) + ->withCount('items') ->get(); }); diff --git a/app/Repositories/Catalog/CategoryRepository.php b/app/Repositories/Catalog/CategoryRepository.php index 0a68140..4f11d6e 100644 --- a/app/Repositories/Catalog/CategoryRepository.php +++ b/app/Repositories/Catalog/CategoryRepository.php @@ -7,12 +7,16 @@ use DB; use Carbon\Carbon; use Image; use Storage; +use Cache; class CategoryRepository { public function getList($params){ - return Category::get(); +return Cache::remember('catalog_category_'.json_encode($params), 60, function () use ($params) { + $limit = $params["limit"] ?? 7; + return Category::take($limit)->withCount('items')->get(); +}); } } diff --git a/app/Repositories/Catalog/GenderRepository.php b/app/Repositories/Catalog/GenderRepository.php index 8846d75..422568e 100644 --- a/app/Repositories/Catalog/GenderRepository.php +++ b/app/Repositories/Catalog/GenderRepository.php @@ -8,11 +8,14 @@ use DB; use Carbon\Carbon; use Image; use Storage; +use Cache; class GenderRepository { public function getList($params){ + return Cache::remember('catalog_gender_'.json_encode($params), 60, function () use ($params) { + $category_id = @$params["category_id"]; $brand_id = @$params["brand_id"]; if ($category_id && $brand_id){ @@ -28,9 +31,10 @@ class GenderRepository where store_category_id = ? and brand = ? and items.is_publish = true ",[$category_id, $brand->name]); $ids = collect($ids)->pluck("gender"); - return Gender::whereIn("name",$ids)->get(); + return Gender::whereIn("name",$ids)->withCount('items')->get(); } - return Gender::get(); + return Gender::withCount('items')->get(); +}); } } diff --git a/app/Repositories/Catalog/ProductRepository.php b/app/Repositories/Catalog/ProductRepository.php index 43f8dd9..3e18e6e 100644 --- a/app/Repositories/Catalog/ProductRepository.php +++ b/app/Repositories/Catalog/ProductRepository.php @@ -62,9 +62,19 @@ class ProductRepository - $builder = Items::select('items.*', 'percent') + $builder = Items::with([ + 'images', + 'variants', + 'reference' +])->select('items.*', 'percent') +->addSelect(DB::raw("coalesce(d.price, coalesce(pricelist.price, net_price)) as discount_price")) +->addSelect(DB::raw("to_qty / from_qty as conversion")) +->leftJoin("item_convertions", function($join){ + $join->on("from_unit","=","items.unit"); + $join->on("to_unit","=","items.display_unit"); +}) ->leftJoin('item_dimension', 'item_dimension.no', 'items.number') - ->leftJoin(DB::raw("(select distinct on (item_id) item_id, percent, discounts.created_at, + ->leftJoin(DB::raw("(select distinct on (item_id) item_id, percent, price, discounts.created_at, case when discounts.valid_at is null then 'special-offer' else 'limited-sale' end as event from discount_items left join item_reference on item_reference_id = item_reference.id @@ -74,6 +84,17 @@ class ProductRepository ( discounts.valid_at is null or discounts.expired_at >= now()) order by item_id, discounts.created_at desc ) as d"), 'd.item_id', '=', 'items.id') + + ->leftJoin(DB::raw("(select distinct on (item_id) item_id, price + from discount_items + left join item_reference on item_reference_id = item_reference.id + left join discounts on discounts.id = discount_id + where discounts.type = 'price' and location_id is null and + ( discounts.valid_at is null or discounts.valid_at <= now()) and + ( discounts.valid_at is null or discounts.expired_at >= now()) + order by item_id, discounts.created_at desc + ) as pricelist"), 'pricelist.item_id', '=', 'items.id') + ->when(true, function ($query) use ($event, $sort, $sorting_ids) { if ($event) { $query->orderByRaw("case when brand = 'CHAMELO' then 1 else 2 end ASC "); @@ -96,7 +117,8 @@ class ProductRepository } elseif ($sort == 'new') { $query->orderByRaw("case when category1 in ('CLUBS','CLUB','COMPONENT HEAD') and brand = 'PXG' then 1 else 2 end ASC"); } else { - $query->orderByRaw('case when d.created_at is not null then 1 else 2 end ASC, random()'); + $query->orderByRaw('case when d.created_at is not null then 1 else 2 end ASC'); + $query->inRandomOrder(); } } @@ -278,7 +300,11 @@ class ProductRepository public function show($slug) { - $product = Items::where('slug', $slug) + $product = Items::with([ + 'images', + 'variants', + 'reference' +])->where('slug', $slug) ->when(is_int($slug), function ($query) use ($slug) { return $query->orWhere('id', $slug); }) @@ -305,6 +331,10 @@ class ProductRepository // Remove except_ids from the list $ids = array_diff($ids, (array) $except_ids); - return Items::whereIn('id', $ids)->inRandomOrder()->take(10)->get(); + return Items::with([ + 'images', + 'variants', + 'reference' +])->whereIn('id', $ids)->inRandomOrder()->take(10)->get(); } } diff --git a/app/View/Components/FooterBrands.php b/app/View/Components/FooterBrands.php index 16b6afb..a0cac49 100644 --- a/app/View/Components/FooterBrands.php +++ b/app/View/Components/FooterBrands.php @@ -16,7 +16,7 @@ class FooterBrands extends Component */ public function __construct(BrandRepository $brandRepository) { - $this->brands = $brandRepository->getList([]); + $this->brands = $brandRepository->getList(["limit"=>10]); } /** diff --git a/app/View/Components/FooterCategory.php b/app/View/Components/FooterCategory.php index 5a9e014..7054251 100644 --- a/app/View/Components/FooterCategory.php +++ b/app/View/Components/FooterCategory.php @@ -16,7 +16,7 @@ class FooterCategory extends Component */ public function __construct(CategoryRepository $categoryRepository) { - $this->categories = $categoryRepository->getList([]); + $this->categories = $categoryRepository->getList(['limit' => 7]); } /** diff --git a/app/View/Components/Grocery/FeaturedCategories.php b/app/View/Components/Grocery/FeaturedCategories.php index f036638..20cdebf 100644 --- a/app/View/Components/Grocery/FeaturedCategories.php +++ b/app/View/Components/Grocery/FeaturedCategories.php @@ -16,7 +16,7 @@ class FeaturedCategories extends Component */ public function __construct(GenderRepository $genderRepository) { - $this->genders = $genderRepository->getList([]); + $this->genders = $genderRepository->getList(['limit' => 7]); } /** diff --git a/app/View/Components/Grocery/HeaderCategoryDropdown.php b/app/View/Components/Grocery/HeaderCategoryDropdown.php index 23cc476..d948e68 100644 --- a/app/View/Components/Grocery/HeaderCategoryDropdown.php +++ b/app/View/Components/Grocery/HeaderCategoryDropdown.php @@ -18,8 +18,8 @@ class HeaderCategoryDropdown extends Component */ public function __construct(CategoryRepository $categoryRepository, GenderRepository $genderRepository) { - $this->categories = $categoryRepository->getList([]); - $this->genders = $genderRepository->getList([]); + $this->categories = $categoryRepository->getList(['limit'=>7]); + $this->genders = $genderRepository->getList(['limit'=>7]); } /** diff --git a/app/View/Components/Grocery/PopularProducts.php b/app/View/Components/Grocery/PopularProducts.php index ed159dc..c0f08a7 100644 --- a/app/View/Components/Grocery/PopularProducts.php +++ b/app/View/Components/Grocery/PopularProducts.php @@ -21,7 +21,7 @@ class PopularProducts extends Component { // - $this->categories = $categoryRepository->getList([]); + $this->categories = $categoryRepository->getList(['limit'=>7]); $this->products = $productRepository->getList([ 'limit' => 12, ]); diff --git a/app/View/Components/Grocery/TopHeader.php b/app/View/Components/Grocery/TopHeader.php index 5d5bde9..2acc2d8 100644 --- a/app/View/Components/Grocery/TopHeader.php +++ b/app/View/Components/Grocery/TopHeader.php @@ -21,8 +21,7 @@ class TopHeader extends Component */ public function render(): View|Closure|string { - $brands = app(\App\Repositories\Catalog\BrandRepository::class)->getList([]); - + $brands = app(\App\Repositories\Catalog\BrandRepository::class)->getList(["limit"=>10]); return view('components.grocery.top-header', compact('brands')); } } diff --git a/app/View/Components/Layout/NavbarCategory.php b/app/View/Components/Layout/NavbarCategory.php index 1642d48..d21d63d 100644 --- a/app/View/Components/Layout/NavbarCategory.php +++ b/app/View/Components/Layout/NavbarCategory.php @@ -28,15 +28,12 @@ class NavbarCategory extends Component $brandsRepository = new BrandRepository(); - $this->genders = $genderRepository->getList([]); - $this->genders = collect($this->genders)->chunk(7); + $this->genders = $genderRepository->getList(['limit' => 7]); - $this->categories = $categoryRepository->getList([]); + $this->categories = $categoryRepository->getList(['limit' => 7]); // chunk - $this->categories = collect($this->categories)->chunk(7); - $this->brands = $brandsRepository->getList([]); - $this->brands = collect($this->brands)->chunk(7); + $this->brands = $brandsRepository->getList(['limit' => 10]); } /** diff --git a/app/View/Components/Navbar/Categories.php b/app/View/Components/Navbar/Categories.php index 42edec5..8583c05 100644 --- a/app/View/Components/Navbar/Categories.php +++ b/app/View/Components/Navbar/Categories.php @@ -18,7 +18,7 @@ class Categories extends Component public function __construct() { $categoryRepository = new CategoryRepository(); - $this->categories = $categoryRepository->getList([]); + $this->categories = $categoryRepository->getList(['limit'=>7]); } /** diff --git a/resources/views/components/grocery/featured-categories.blade.php b/resources/views/components/grocery/featured-categories.blade.php index d88d7b1..717546c 100644 --- a/resources/views/components/grocery/featured-categories.blade.php +++ b/resources/views/components/grocery/featured-categories.blade.php @@ -29,7 +29,7 @@
{{ $value->productCount() }} products
+{{ $value->items_count }} products