app\Category::progeny PHP Method

progeny() public static method

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 static function progeny($id, &$list, $fields = ['id', 'name'])
    {
        $childs = self::childsOf($id)->select($fields)->get();
        if (is_null($childs)) {
            return;
        }
        foreach ($childs as $value) {
            $list[] = $value->toArray();
            self::progeny($value->id, $list, $fields);
        }
    }

Usage Example

Example #1
0
 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;
         }
     }
 }
All Usage Examples Of app\Category::progeny