Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

query display particular subcategory for a particular category

Hi,

below is the table which has id,name,parent_id. the parent_id is the foreign key which refers to id column (categories and subcategories)

id   name                                             parent_id
1   Elect                                              NULL 
2   Cam                                                 NULL    
3   Appliances                                     NULL 
4   Smart Phone                                 1   
5   Tablets                                         1   
6   Refrigi                                                3

i have queried from the database to display the categories using below query

DB::table('categories')->select('name')->where('parent_id', '!=', NULL)->get();

above query display the categories name in the drop down

how to query to display the subcategories name based on particular category

for example if the Appliances is the category when the user clicks on the drop down it should display the Refrigi as the subcategory only. any help.??

Thanks,

0 likes
8 replies
tisuchi's avatar

If you get you correctly, you can use relationships. A category has many sub category.

For dropdown menu, I think you have to use any standard JS library to synchronize that.

Sergiu17's avatar

Hi, if you have just on level of dropdown, then is pretty easy.

$categories = DB::table('categories')->select('name')->where('parent_id', '=', NULL)->get();
$subcategories = DB::table('categories')->select('name')->where('parent_id', '!=', NULL)->get();

and in your views

@foreach ($categories as $category)

    {{ $category->name }}

    @foreach ($subcategories as $subcategory)

        @if ( $subcategory->parent_id == $category->id )

            {{ $subcategory->name }}

        @endif

    @endforeach 

@endforeach 

p.s. I didn't test this)

1 like
Shawdow's avatar

@Sergiu17 shows me error but that property exists in the database

Undefined property: stdClass::$parent_id

Shawdow's avatar

@tisuchi according to your solution i need to use two different table for categories and subcategories ??

Shawdow's avatar

@Sergiu17 It was wrong in the query that i have tried sir

$categories = DB::table('categories')->select('*')->where('parent_id', '=', NULL)->get();
$subcategories = DB::table('categories')->select('*')->where('parent_id', '!=', NULL)->get();

but the results not show properly if i try to hover on Elect it shows me Refrigi not was not expected. expected was Smart Phone and Tablets

Shawdow's avatar

@tisuchi thank you sir i have tried your solution that works with the tinker also.but can you give the description how should i implement for my solution for displaying sub-categories for a particular category!!

Shawdow's avatar

@tisuchi i have tried like below when the users over the categories name it should show the list of subcategories for a particular category name

but the results not show properly if i try to hover on Elect it shows me Refrigi not was not expected. expected was Smart Phone and Tablets

<style>


.show-on-hover:hover > ul.dropdown-menu {
    display: block;
  
}

</style>


 <div class="container">
            <div class="row">
            <div class="btn-group show-on-hover">    
@foreach ($categories as $category)
              <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
               {{ $category->name }} <span class="caret"></span>
              </button>
            
    @foreach ($subcategories as $subcategory)
              <ul class="dropdown-menu" role="menu">
                              @if ( $categories->parent_id == $subnames->id )
                <li><a href="">{{ $subcategory->name }}</a></li>
                @endif
              </ul>
                        @endforeach
        @endforeach
            </div>
                    </div>
                 </div>

Please or to participate in this conversation.