progeny
Retrieve all the categories children of one passed through parameter,
checking data from bottom to top.
public static progeny ( [int] $id, &$list, [array] $fields = ['id', 'name'] ) | ||
$id | [int] | |
$fields | [array] |
public function scopeRefine($query, $filters) { foreach ($filters as $key => $value) { switch ($key) { case 'category': $children = \Cache::remember('progeny_of_' . $value, 15, function () use($value) { Category::progeny($value, $children, ['id']); return $children; }); $children[] = ["id" => $value * 1]; $query->whereIn('category_id', $children); break; case 'conditions': $query->where('condition', 'LIKE', $value); break; case 'brands': $query->where('brand', 'LIKE', $value); break; case 'min': case 'max': $min = array_key_exists('min', $filters) ? trim($filters['min']) != '' ? $filters['min'] : '' : ''; $max = array_key_exists('max', $filters) ? trim($filters['max']) != '' ? $filters['max'] : '' : ''; if ($min != '' && $max != '') { $query->whereBetween('price', [$min, $max]); } elseif ($min == '' && $max != '') { $query->where('price', '<=', $max); } elseif ($min != '' && $max == '') { $query->where('price', '>=', $min); } break; default: if ($key != 'category_name' && $key != 'search' && $key != 'page') { //changing url encoded character by the real ones $value = urldecode($value); //applying filter to json field $query->whereRaw("features LIKE '%\"" . $key . "\":%\"%" . str_replace('/', '%', $value) . "%\"%'"); } break; } } }
/** * $id category to searh progeny , $list array to use, $fields that you need */ public static function progeny($id, &$list, $fields = ['id', 'name']) { $childs = Category::childsOf($id)->select($fields)->get(); if (is_null($childs)) { return; } foreach ($childs as $value) { $list[] = $value->toArray(); Category::progeny($value->id, $list, $fields); } return; }
/** * countingProductsByCategory * Products total by category collection * @param [type] $all_products refine products * @param [type] $categories refine categories * @return [array] filter used in the product list view menu */ 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; }