Home > Enterprise >  Laravel Mega Menu Loop
Laravel Mega Menu Loop

Time:10-14

I am learning laravel and was trying to create a Megamenu. But I can call parent menu, I stacked at 1st child and 2nd child.

Same children coming under every parent.

<div class="navbar-mega">
    <div class="dropdown-mega">
        @php
        $categories = App\Models\Category::where('parent_id', 0)->orderBy('order_level','ASC')->get();
        @endphp
        @foreach ($categories as $item)
            <button class="dropbtn-mega"> {{$item->id}}</button>
        @endforeach
        <div class="dropdown-content-mega">
            <div class="row-mega">
                @php
                $subcategories = App\Models\Category::where('parent_id',
                $item->id)->orderBy('name','ASC')->get();
                @endphp
                @foreach ($subcategories as $subcategory)
                    <div class="column-mega">
                        <h3>{{$subcategories}}</h3>
                        <a href=""></a>
                    </div>
                @endforeach
            </div>
        </div>
    </div>
</div>

Database Fields

Subcategory Query Output

CodePudding user response:

The better way to use below code in controller. And create the relations in model.

$categories = App\Models\Category::where('parent_id', 0)->orderBy('order_level','ASC')->get();

Use child parent relation in your model:

public function children()
{
    return $this->hasMany(Category:Class,'parent_id');
}

And use with to get as parent-child tree

$categories = App\Models\Category::with('children)->where('parent_id', 0)->orderBy('order_level','ASC')->get();

You can check it using

dd($categories);

and use loop on it easily.

CodePudding user response:

This code :

$subcategories = App\Models\Category::where('parent_id',$item->id)->orderBy('name','ASC')->get();

is not within the loop through the $categories as $item. So when it is executed it will also return the same set of categories (as $item will always be the same value at that point in your code.

Put it within the loop :

@foreach ($categories as $item)
    <button class="dropbtn-mega"> {{$item->id}}</button>
    <div class="dropdown-content-mega">
        <div class="row-mega">
            @php
            $subcategories = App\Models\Category::where('parent_id',
            $item->id)->orderBy('name','ASC')->get();
            @endphp
            @foreach ($subcategories as $subcategory)
                <div class="column-mega">
                    <h3>{{$subcategories}}</h3>
                    <a href=""></a>
                </div>
            @endforeach
        </div>
    </div>
@endforeach

and it should work fine.

CodePudding user response:

You can use append for fetch sub categories, in your category model use this,

protected $appends = [
    'sub_categories'
];


public function getSubCategoriesAttribute()
{
    return Category::where('parent_id', $this->id)->orderBy('name','ASC')->get();
}
<div class="navbar-mega">
    <div class="dropdown-mega">
        @php
        $categories = App\Models\Category::where('parent_id', 0)->orderBy('order_level','ASC')->get();
        @endphp
        @foreach ($categories as $category)
            <button class="dropbtn-mega"> {{ $category->id }}</button>
        @endforeach
        <div class="dropdown-content-mega">
            <div class="row-mega">
                @foreach ($category->sub_categories as $subCategory)
                    <div class="column-mega">
                        <h3>{{ $subCategory->name }}</h3>
                        <a href=""></a>
                    </div>
                @endforeach
            </div>
        </div>
    </div>
</div>

CodePudding user response:

I think your table structure is not appropriate. Maybe you are trying to create an infinity category, subcategory, and other staff. In that case, you may go through this.

Table columns name should be like

id parent_id level name .....

Brief :

id as primary key, parent_id will be foreign key of your db table primary id, and other columns will be as your expectation.

How to read all the staff?

You may follow the below steps.

$categories = Category::query()->where('parent_id',0)->get() // You may put order by something.

Note: What you will get from this query? The answer is, you will get all the parent's categories.

Now read every single parents category and their childrens.

How to read parents' category and their children?

At First Read Parent category

foreach($categories as $key=>$category){
    echo $category->name; // Your Parent Category is here.
}

Secondly, Read the Parent-Child category

foreach($categories as $key=>$category){
    echo $category->name; // Your Parent Category is here.
    // Must check is child category found or not. Otherwise your error can be arrise.
    if($categories->subCategories->count()){
        foreach($categories->subCategories as $k=>$subCategory){
             echo $subCategory->name;
        }
    }
}

A question can be here $categories->subCategories->count() && foreach($categories->subCategories as $k=>$subCategory) ?

If you have a question like this.

The answer is below :

Note: You must add a relationship method inside Category Model like A Parent Category can be More Child SubCategory

add this method to your Category Model.

public function subCategories(){
   return $this->hasMany(Category::class,'parent_id');
}

Now you will get smooth output.

Have fun.

  • Related