Garg Garg - 1 year ago 67
PHP Question

Showing all subcategories when click on main category in Laravel

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 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>

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

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

Answer Source

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

$subcats = SubCategories::where('category_id', '=', $categoryId)->get();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download