Saturday, April 1, 2017

Call to undefined method stdClass::checkSubcategory()

I was trying to make a dynamic Menu where in menu section i want to show a category and if the category has any subcategory I wanted to show it under it..

I have read the following method on my category model

public static function checkSubcategory($id){
        $category = Category::find($id);
        $id = $category->id;
        $subcategories = \DB::table ('subcategories')
                    ->join('categories_subcategories','subcategories.id','=','categories_subcategories.subcategory_id')
                    ->join('categories','categories_subcategories.category_id','=','categories.id')
                    ->where('categories.id','=',$id)
                    ->select('subcategories.name')
                    ->get();    
         return $subcategories;
    }   

And here is the View

@foreach($categories as $row)
                @if($row->checkSubcategory($row->id))       
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle">
                        
                    </a>
                <ul class="dropdown-menu">
                    <li>
                    @foreach($row->checkSubcategory($row->id) as $sub)
                    <a href=""></a>
                    @endforeach 
                    </li>                                               
                </ul>
                </li>
                    @else 
                <li class="pull-right"><a href="#"></a></li>
                    @endif      
                @endforeach

But the problem is when ever i tried to load the view i see the following Error

ErrorException in efd7d9fe55aa0c18c60d0a857a8a44ed1725a390.php line 213:
Call to undefined method stdClass::checkSubcategory()

What could be reason for the error..any suggestion please.



via User57

Advertisement