1
votes

I'm trying to make function where when I click on main category name should load new page and display all subcategories if there are some.

So far I have this in my route

Route::get('/subcatView/{categoryId}', ['uses' => 'CategorySingleProducts@showSubCats']);

This is button in my index view where I click to load sub-cats

<a href="{{ URL::to( '/subcatView/' .  $category_menu->category_id) }}">{{ $category_menu['category_name'] }}></a>

This is from my controller

public function showSubCats($categoryId) {

      $subcats = SubCategories::where('category_id', '=', $categoryId)->first();
      return View::make('site.subcategory', [            
          'subcats' => $subcats
      ]); 
} 

and subcategory blade

@foreach($subcats as $i => $subcategory)   

     <div class="col-md-4">
          <div class="panel panel-default text-center">                              
               <div class="panel-heading">{{ $subcategory['sub_cat_name'] }}</div> 
                   <div class="panel-body min-h-230">
                           <img src="{{ $subcategory['sub_cat_image'] }}" alt="{{ $subcategory['sub_cat_name'] }}" />
                   </div>
                   <div class="panel-footer">
                       <div class="row">                                    
                          <div class="col-sm-6 text-right">
                    <a href="{{ URL::to( '/single/products/' .  $subcategory['sub_cat_id']) }} " class="btn btn-info view-more">View More</a>
                           </div>
                        </div>
                   </div>
                </div>
          </div>
@endforeach

In sub_category table I have column category_id which is equal to main category id.

When I open the page now I don't get anything for sub categories. no sub_cat_name, sub_cat_image... nothing. Just empty html style

1
Why don't dd($subcats) in showSubCats before return viewARIF MAHMUD RANA

1 Answers

2
votes

Your subcats query in the controller should request get (an array) not first (single object).

$subcats = SubCategories::where('category_id', '=', $categoryId)->get();