Monday, February 27, 2017

Dynamic Subcategories Navigation with category and subcategory slug laravel

I have a table in MySQl that contains list of all categories and subcategories. Each subcategory includes assigned parent_category_id
The page controller is as follows:
Route::get('/', 'PageController@home');
Route::get('{category}', 'PageController@category_show');
Route::get('{category}/{subcategory}', 'PageController@subcategory_show');

Now to the main part of the question, I am trying to create Top Navigation and Sidebar Navigation for subcategories. The top navigation is pretty simple and this is how I managed to accomplish this in my NavigationComposer.php
class NavigationComposer
{
    public function compose(View $view)
    {
        $view->with('categories', \App\Category::where(
            array(
            'category_display_type' => 'header',
            'category_visibility' => 1
        ))->get());
    }
}

I have tried to do the same with my subcategories in my SidebarnavComposer.php All works just fine except that I need to have the category slug in the URI when I switch to one of the categories. So far I am only able to get parent_category_id in URI. I have tried to create a join, but getting some errors.
Here is the content of my SidebarnavComposer.php
class SidebarnavComposer
{
    public function compose(View $view)
    {
        $view->with('subcategories', \App\Category::where(
            array(
            'category_display_type' => 'sidebar',
            'category_visibility' => 1
        ))->get());

    }

}

I am new to Laravel so please help me if you can.
Thanks in advance


via AlexB

Advertisement