app\helpers\productsHelper::countingProductsByCategory PHP 메소드

countingProductsByCategory() 공개 정적인 메소드

countingProductsByCategory Products total by category collection.
public static countingProductsByCategory ( [type] $all_products, [type] $categories ) : [array]
$all_products [type]
$categories [type]
리턴 [array]
    public static function countingProductsByCategory($all_products, $categories)
    {
        $filters = ['category' => []];
        foreach ($categories as $value) {
            $category_id = $value['id'];
            $childs = \Cache::remember('progeny_of_' . $category_id, 15, function () use($category_id) {
                Category::progeny($category_id, $childs, ['id']);
                return $childs;
            });
            $all = $childs;
            $childs = [];
            foreach ((array) $all as $val) {
                $childs[] = $val['id'];
            }
            $qty = 0;
            if ($all_products) {
                $qty = $all_products->where('category_id', $category_id)->count();
                $qty += $all_products->filter(function ($item) use($childs) {
                    return in_array($item->category_id, $childs);
                })->count();
            }
            if ($qty) {
                $filters['category'][$category_id]['id'] = $category_id;
                $filters['category'][$category_id]['name'] = $value['name'];
                $filters['category'][$category_id]['qty'] = $qty;
            }
        }
        //Order by qty
        if (isset($filters['category'])) {
            $filters['category'] = collect($filters['category'])->sortByDesc('qty');
        }
        return $filters;
    }

Usage Example

예제 #1
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index(Request $request)
 {
     /**
      * $refine
      * array that contains all the information retrieved through the URL
      * array_unique is applied to avoid redundant variables.
      *
      * @var array
      */
     $refine = \Utility::requestToArrayUnique($request->all());
     /**
      * $search
      * this var contains the information typed into search box.
      *
      * @var [type]
      */
     $search = $request->get('search');
     /**
      * $products
      * Filtered products list.
      *
      * @var [type]
      */
     $products = Product::select('id', 'category_id', 'name', 'price', 'description', 'condition', 'brand', 'rate_val', 'type', 'features', 'parent_id', 'tags')->search($search, false)->refine($refine)->free()->actives()->orderBy('rate_val', 'desc');
     /**
      * $all_products
      * it is the product list refined, which will be used in each filter process below.
      *
      * @var [type]
      */
     $all_products = $products->get();
     /**
      * $suggestions
      * Array which contains the user product suggestions.
      *
      * @var array
      */
     $suggestions = [];
     if (count($all_products) < 28) {
         $suggestions = productsHelper::suggest('my_searches');
     }
     /*
      * $filters
      * it is the refine menu array, which is used to build the search options
      * @var [type]
      */
     $category_id = $request->get('category') ? $request->get('category') : 'mothers';
     $categories = \Cache::remember('categories_' . $category_id, 25, function () use($category_id) {
         return Category::select('id', 'name')->childsOf($category_id)->actives()->get()->toArray();
     });
     $filters = productsHelper::countingProductsByCategory($all_products, $categories);
     //condition
     $filters['conditions'] = array_count_values($all_products->lists('condition')->toArray());
     //brand filter
     $filters['brands'] = array_count_values($all_products->lists('brand')->toArray());
     //features
     $features = [];
     $irrelevant_features = ['images', 'dimensions', 'weight', 'brand'];
     //this has to be in company setting module
     foreach ($all_products->lists('features') as $feature) {
         $feature = array_except($feature, $irrelevant_features);
         foreach ($feature as $key => $value) {
             $features[$key][] = $value;
         }
     }
     //products by feature
     foreach ($features as $key => $value) {
         foreach ($features[$key] as $row) {
             if (!is_array($row)) {
                 $filters[$key][$row] = !isset($filters[$key][$row]) ? 1 : $filters[$key][$row] + 1;
             }
         }
     }
     //prices filter
     $prices = $all_products->lists('price', 'price')->toArray();
     sort($prices);
     //saving tags from searching products in users preferences
     if ($search != '') {
         $my_searches = [];
         $cont = 0;
         foreach ($all_products as $product) {
             if (trim($product->tags) != '') {
                 $my_searches = array_merge($my_searches, explode(',', $product->tags));
             }
             if ($cont++ == 10) {
                 break;
             }
         }
         if (count($my_searches) > 0) {
             UserController::setPreferences('my_searches', $my_searches);
         }
     }
     $products = $products->paginate(28);
     $panel = $this->panel;
     $panel['left']['class'] = 'categories-panel';
     $products->each(function (&$item) {
         if ($item['rate_count'] > 0) {
             $item['num_of_reviews'] = $item['rate_count'] . ' ' . \Lang::choice('store.review', $item['rate_count']);
         }
     });
     return view('products.index', compact('filters', 'products', 'panel', 'listActual', 'search', 'refine', 'suggestions'));
 }