dimanche 12 juillet 2020

Build multiple menu from Laravel array tree

I build menu in Bootstrap 4 with: https://packagist.org/packages/kalnoy/nestedset

I have this code:

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    $table->string('category_name', 155);
    $table->string('description', 155)->nullable();
    $table->string('keywords', 155)->nullable();
    $table->longText('content')->nullable();
    $table->char('enable', 1)->default(0);
    $table->bigInteger('order')->default(0);
    $table->string('slug', 160)->nullable();
    NestedSet::columns($table);
    $table->engine = "InnoDB";
    $table->charset = 'utf8mb4';
    $table->collation = 'utf8mb4_unicode_ci';
});

I have in my controller:

$nodes = Category::where('enable', 1)->get()->toTree()
return view('front.product-list', ['menu'=>$nodes]);

and in view (blade):

<nav class="navbar navbar-expand-lg text-body px-md-0 pb-md-0 navbar-light w-100">
    <a class="navbar-brand" href="/"><img src="" class="pb-2"></a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarmenu"
            aria-controls="navbarmenu" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon "></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarmenu">
        <ul class="navbar-nav">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="dropdown1" data-toggle="dropdown"
                   aria-haspopup="true"
                   aria-expanded="false">Kategorie <i class="fas fa-angle-down"></i></a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                    <div class="dropdown">
                        <a class="dropdown-item dropdown-toggle" href="#" id="navbarDropdown" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown1</a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </div>
                    <a class="dropdown-item" href="#">xxxx</a>
                    <a class="dropdown-item" href="#">xxxx</a>
                    <div class="dropdown">
                        <a class="dropdown-item dropdown-toggle" href="#" id="navbarDropdown2" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown2</a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                            <a class="dropdown-item" href="#">Action2</a>
                            <a class="dropdown-item" href="#">Another action2</a>
                            <div class="dropdown">
                                <a class="dropdown-item dropdown-toggle" href="#" id="navbarDropdown3"
                                   role="button"
                                   data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown3</a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                                    <a class="dropdown-item" href="#">Action 3</a>
                                    <a class="dropdown-item" href="#">Another action 3</a>
                                    <a class="dropdown-item" href="#">Something else here 3</a>
                                </div>
                            </div>
                            <a class="dropdown-item" href="#">Something else here2</a>
                        </div>
                    </div>

                    @foreach($menu as $value)
                        @if(!empty($value->children))
                            <div class="dropdown">
                                <a class="dropdown-item dropdown-toggle" href="#"
                                   id="navbarDropdown" role="button"
                                   data-toggle="dropdown" aria-haspopup="true"
                                   aria-expanded="false"></a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdown">


                                    @foreach($value->children as $value2)
                                        @if(!empty($value2->children))
                                            <div class="dropdown">
                                                <a class="dropdown-item dropdown-toggle" href="#"
                                                   id="navbarDropdown" role="button"
                                                   data-toggle="dropdown" aria-haspopup="true"
                                                   aria-expanded="false"></a>
                                                <div class="dropdown-menu"
                                                     aria-labelledby="navbarDropdown">
                                                    .....next level ......
                                                </div>
                                            </div>
                                        @else
                                            <a class="dropdown-item"
                                               href="#"> </a>
                                        @endif
                                    @endforeach
                                </div>
                            </div>


                        @else
                            <a class="dropdown-item" href="#"> </a>
                        @endif
                    @endforeach

                </div>
            </li>

           
        </ul>
    </div>
</nav>

I have problem with this part of code:

@foreach($value->children as $value2)
    @if(!empty($value2->children))
        <div class="dropdown">
            <a class="dropdown-item dropdown-toggle" href="#"
               id="navbarDropdown" role="button"
               data-toggle="dropdown" aria-haspopup="true"
               aria-expanded="false"></a>
            <div class="dropdown-menu"
                 aria-labelledby="navbarDropdown">
                .....next level ......
            </div>
        </div>
    @else
        <a class="dropdown-item"
           href="#"> </a>
    @endif
@endforeach

I have 18 menu levels. Do I have to copy the above code 18 "to build this menu in" ..... next level ...... "? Is there a more universal way?

Please help me.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/32g5ezD
via IFTTT

Aucun commentaire:

Enregistrer un commentaire