dimanche 1 juillet 2018

Laravel: show categories and products tree wise

I wanted to show the categories and products as tree wise means subsequent products will be shown under the category and so on. To create this, I made this script which shows only categories but no products appearing under it. Below is my script for the controller:

$data['sitecategories'] = $this->generateSiteCategorySubcategory(-1);

In the same controller, I have created the function:

public function generateSiteCategorySubcategory($startAt) {
        if ($children = Sitecategory::getCategorySubcategory($startAt)) {
            $thisLevel = array();
            foreach ($children as $child) {
                $thisLevel[$child->id] = $child;
                $thisLevel[$child->id]->children = $this->generateSiteCategorySubcategory($child->id);
            }
            return $thisLevel;
        }
    }

In the blade I have written to render the products and categories in select box:

<select name="categoryid"  class="form-control customSelect" >                                                                                        
 @foreach ($sitecategories as $sitecategory)                                                                                    
 @php if($sitecategory->parentCategoryId != '-1'){ $catName = "--". 
 $sitecategory->categoryName;                                                                                        
 } else { $catName = $sitecategory->categoryName;                                                                                     
}                                                                                    
@endphp
<option value=""></option>
@endforeach
</select>

Below is the db table structure:

id parentCategoryId categoryName status


1 -1 Tablets & Phones 1

2 -1 Fashion 1

10 1 Phone 1

12 1 tablets 1

15 2 Women's 1

Once I run the script, it shows only categories in the select box but no products are appearing under categories.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire