0

I have a question. Before asking this question I have searched a lot but I did not found any workable example, so I have put my question here. I have a concept of parent child relationship with in the same table for example, I have table category like this:

id int auto_increment primary
name varchar
type varchar
parentid int 

Now I want to generate select box with multi level option like:

<select class="category" name="category">
   <option value="-1">Select Category</select>
   <optgroup value="0" label="Parent Tag">
      <option value="1">Child Tag</option>
      <option value="2">Child Tag</option>
   </optgroup>
      <optgroup value="3" label="Parent Tag">
      <option value="4">Child Tag</option>
      <option value="5">Child Tag</option>
   </optgroup>
</select>

I also want to generate Menus same as above.

<ul>
<li>Menu 1</li>
<li>
   Menu 2
   <ul class="dropdown">
     <li>Menu 1 of 2</li>
     <li>Menu 2 of 2</li>
     <li>Menu 3 of 2</li>
   </ul>
</li>
<li>Menu 3</li>
<li>
menu 4
   <ul class="dropdown">
     <li>Menu 1 of 4</li>
     <li>Menu 2 of 4</li>
     <li>Menu 3 of 4</li>
   </ul>
</li>
</ul>

Please friends help to achieve this functionality in laravel 5. Depth for this is not limited and it can be 2, 3, 4; as many as the user wants to add.

Model
class Category extends Model{
...
public function parent()
    {
        return $this->belongsTo('App\Models\Category', 'parentid');
    }

    public function children()
    {
        return $this->hasMany('App\Models\Category', 'parentid');
    }
}

In Controller
$categories = Category::with('children')->select('name', 'id','parentid')->where('configtype','=','Category')->get();
return view('admin.category.index',['pageTitle' => 'Category', 'configlist' => $categories]);

In view
?
Manoj Sharma
  • 596
  • 1
  • 6
  • 23

1 Answers1

3

For the ul you can create a recursive function using blade e.g.

@include('partials.menu', $items)

Then in that view something like:

<ul>
    {{-- You would have to provide your own logic to decide which class name the ul should have --}}
    @foreach($items as $item)

        {{ $item->name }}
        @if(!empty($item->children)) {{-- Or however you want to check for children --}}
            @include('partials.menu', ['items' => $item->children]) {{-- Here I am just telling blade to treat the children as $items where they are passed through --}}
        @endif
    @endforeach
</ul>

This is a basic implementation of a recursive function with blade.

You can use a similar approach for the select as well.

Hope this helps!

Rwd
  • 34,180
  • 6
  • 64
  • 78
  • I have tried, but i am getting View [partials.menu] not found. Now what to do in view how to design it. i have also updated model and controller code. Please review and suggest me the how to design views. If i create partial.menu or menu with in categories view than what to write in this view. – Manoj Sharma Aug 24 '15 at 12:22
  • 1
    Have you actually created the file? – Rwd Aug 24 '15 at 12:23
  • Not found problem in resolved after creating the file, but the question what to write there ? Please read my updated question for reference. Thanks for your valuable response. – Manoj Sharma Aug 24 '15 at 12:26
  • The section that I wrote under `Then in that view something like:`. You may also want to look at this package https://github.com/etrepat/baum. – Rwd Aug 24 '15 at 12:35
  • @ManojSharma did this answer your question? – Rwd Aug 24 '15 at 16:07
  • Actually not working and don't want to use any third party lib. for this small work as i am not going to use at very high level. I thing there is a problem in my model, can you please go through with that as i am not getting proper data as expected. – Manoj Sharma Aug 25 '15 at 05:02
  • It start working, I have just modified my Model according to this solution http://stackoverflow.com/questions/21305111/how-to-create-a-database-driven-multi-level-navigation-menu-using-laravel and combined it with your solution it start working. Thank buddy. – Manoj Sharma Aug 25 '15 at 05:33
  • I have another question. How to indent sub category by one space each based on depth of sub category. – Manoj Sharma Aug 25 '15 at 09:14
  • As this is a new question, you should really create a new question. That being said, the easiest way would be to use `css`. – Rwd Aug 25 '15 at 10:08
  • Ok, I am posting please look into. – Manoj Sharma Aug 25 '15 at 10:32
  • Please check this http://stackoverflow.com/questions/32201890/how-to-indent-option-in-select-or-put-hyphens-before-each-sub-categories-option i have post new question. – Manoj Sharma Aug 25 '15 at 10:43