Support the ongoing development of Laravel.io →
Database Views Blade

My requirement is to construct the treeview using database values.(I'm not using eloquent here. Instead of eloquent I'm using Illuminate\Support\Facades\DB)

Here are my database tables:

    Categories                 sub_categories   
      id(pk)                      id(pk)              
    cate_name                 sub_cat_name   
   route_name                   route_name       
                             Categories_id(fk)

I'm getting categories and sub categories as well which are related to categories table.

Here is my Controller code:

$treeView = DB::table('categories')
                ->join('sub_categories', 'sub_categories.categories_id', '=', 'categories.id')
                ->get();

*Here is the HTML structure in the .blade.php :

@foreach($treeView as $tv)
                <li class="treeview">
                    <a href="#"><i class="fa fa-link"></i> <span>{{ $tv->category_name }}</span> <i
                                class="fa fa-angle-left pull-right"></i></a>
                    <ul class="treeview-menu">
                        <li class=""><a href="#">{{$tv->sub_category_name}}</a></li>
                        <li><a href="#">Update Article</a></li>
                    </ul>
                </li>
            @endforeach

But it doesn't work fine. It gives same main category again and again.. Can anyone suggest a proper way to retrieve data?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.