1

Model:

class Menu extends Model
{
    protected $table = 'menus';

    public function sub_menu(){
        return $this->hasMany('App\Menu', 'parent_id');
    }

}

Controller:

class MenuController extends Controller
{

    public function index()
    {
        $menus = Menu::where('parent_id', 0)->get();
        return view('admin.menus.index',compact('menus'));
    }
}

View:

<ul>
    @foreach($menus as $menu)
    <li>{{ $menu->title }}
    @if($menu->sub_menu->count() > 0)
       <ul>
       @foreach($menu->sub_menu as $sub)
           <li{{ $sub->title }}</li>
       @endforeach
       </ul>
   @endif
   </li>
   @endforeach
</ul>

Table Structure:

----------------------------------------------------------------------
        id          |          title            |      parent_id
----------------------------------------------------------------------
        1           |          Home             |          0
----------------------------------------------------------------------
        2           |        Product1           |          0
----------------------------------------------------------------------
        3           |       Product1.1          |          2
----------------------------------------------------------------------
        4           |       Product1.1.1        |          3
----------------------------------------------------------------------
        5           |       Product1.1.2        |          3
----------------------------------------------------------------------

It's show only 2 levels but I need multiple levels, please help!

5
  • What is the problem here? Commented Oct 29, 2016 at 16:24
  • @AmitGupta.op is aking if the menus are multi level then he need to display menu in loop but he is using foreach loop it will end up with two step menu.i hope he need to use while loop Commented Oct 29, 2016 at 16:26
  • @AmitGupta, It's show only 2 levels but I need Multiple levels. please help! Commented Oct 29, 2016 at 16:30
  • Didn't get you. Can you show in your question what you have got and what you need? Commented Oct 29, 2016 at 16:41
  • @AmitGupta Ok I've just edited my question already. thanks for your comments. Commented Oct 29, 2016 at 16:46

1 Answer 1

2

You can try it using recursion as:

<ul>
    @foreach($menus as $menu)
        <li>
            {{ $menu->title }}
            @if($menu->sub_menu->count())
                {!! view('admin.menus.index', ['menus' => $menu->sub_menu]) !!}
            @endif
     </li>
    @endforeach
</ul>

It will work when your view has only the above content otherwise, you have create new partial and call it recursively.

Just be careful you may end in an infinite loop using recursion.

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

2 Comments

No, it doesn't work for multiple levels but for 1 level work fine. thank you so much but Now I solved it already by create function recursive in model. thanks!
You can check this repo also. Looks it will help you.

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.