0

My requirement is to construct the treeview using database values.

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?

5
  • Are you using eloquent? What are the model names? Commented Jul 19, 2015 at 9:23
  • no .. I'm not using eloquent... I'm using Illuminate\Support\Facades\DB. Commented Jul 19, 2015 at 9:39
  • I have my own database.. So I' not using Eloquent.. Commented Jul 19, 2015 at 9:45
  • Well, it's not that you cannot have both ;) Please check back in half a day, I'll apot an answer when i am at the computer if noone answers by then Commented Jul 19, 2015 at 9:58
  • K.. Thanks.. I m looking forward to hear from u soon... Commented Jul 19, 2015 at 10:00

1 Answer 1

4

I suggest you use Eloquent, as it will make the code simpler and will make your life easier in the future.

Create model classes for your tables:

class Category extends Illuminate\Database\Eloquent\Model {
  protected $table = 'Categories';

  public function subcategories() {
    return $this->hasMany(Subcategory::class, 'Categories_id');
  }
}

class Subcategory extends Illuminate\Database\Eloquent\Model {
  protected $table = 'sub_categories';

  public function category() {
    return $this->belongsTo(Category::class, 'Categories_id');
  }
}

In your controller fetch data like that:

$treeView = Category::with(['subcategories'])->get();

And then in the view:

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

I can see that your categories and subcategories have the same stucture. You might consider storing them in the same table, just add a parent_id field to the table and set it to NULL for parent categories and to parent id for subcategories.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your quick reply.. I tried using your code example. But It gives me the error BadMethodCallException in Builder.php line 2003: Call to undefined method Illuminate\Database\Query\Builder::all()
I created Category and Subcategory classes in app/ directory as u mentioned...
I wanted to check if the category has subcategories else do another work.. I tried it like below. But it didnt retrieve the required answer .. Is anything wrong with it ??
@foreach($treeView as $category) @if($category->has('subcategories')) <li class="treeview"> <a href="#"><i class="fa fa-link"></i> <span>{{ $category->category_name }}</span> <i class="fa fa-angle-left pull-right"></i></a> <ul class="treeview-menu"> @foreach($category->subcategories as $subcategory) <li class=""><a href="#">{{$subcategory->sub_category_name}}</a></li> @endforeach </ul> </li> @else <li><a href="#"><i class="fa fa-link"></i> <span>{{$category->category_name }}</span></a></li> @endif @endforeach
Where did you get that has() idea? Anyway, see the answer in stackoverflow.com/questions/31509536/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.