use App\Models\Product;
use App\Models\ProductType;
use Cubist\Backpack\app\Magic\PageData;
+use Cubist\Backpack\Facades\App;
class Category extends Base
{
public function setData(&$data)
{
- parent::setData($data);
+ \Barryvdh\Debugbar\Facade::startMeasure('set cat data');
$cat = $data['page']->get('category');
- $productType = ProductType::find($cat);
-
- if (!$productType) {
+ $d = cache()->remember('cat_data_' . $cat . '_' . App::getVariant() . '_' . App::getLocale(), 3600, function () use ($cat) {
+
+ $res = [];
+ $productType = ProductType::find($cat);
+ if (!$productType) {
+ return false;
+ }
+ $res['product_type'] = $productType->getPageData();
+
+ $products = Product::with('media') // Eager load media to avoid N+1 query problem
+ ->whereVariant()
+ ->where('product_type', $cat)
+ ->where('online', 1)
+ ->where('public', 1)
+ ->orderBy('reference')
+ ->get();
+
+ $res['products'] = PageData::fromEntities($products);
+
+ // Get available filters
+ $filters = Product::getFilteredProducts($cat);
+ $res['filters'] = $filters ? $filters['filters'] : []; // To be used by Vue component
+ $res['filter_results'] = $filters ? $filters['results'] : [];
+ return $res;
+ });
+ if (!$d) {
$this->_404();
}
- $data['product_type'] = $productType->getPageData();
-
- $products = Product::with('media') // Eager load media to avoid N+1 query problem
- ->whereVariant()
- ->where('product_type', $cat)
- ->where('online', 1)
- ->where('public', 1)
- ->orderBy('reference')
- ->get();
-
- $data['products'] = PageData::fromEntities($products);
-
- // Get available filters
- $filters = Product::getFilteredProducts($cat);
- $data['filters'] = $filters ? $filters['filters'] : []; // To be used by Vue component
- $data['filter_results'] = $filters ? $filters['results'] : [];
+
+ foreach ($d as $k => $v) {
+ $data[$k] = $v;
+ }
+ parent::setData($data);
+
+ \Barryvdh\Debugbar\Facade::stopMeasure('set cat data');
}
}
$root = $menu->getRoot();
+ \Barryvdh\Debugbar\Facade::startMeasure('set products nav');
+ $products = self::_getProducts($menu->getVariant(), $menu->getLocale());
+
+ $root = $menu->getRoot();
+
foreach ($products as $product) {
$product->setLocale('fr');
$detail->addUrlAliases([$slug_fr]);
$menu->addChild($detail);
}
+ \Barryvdh\Debugbar\Facade::stopMeasure('set products nav');
}
/**