malik zubair
malik zubair

Reputation: 15

How to display subcategory related to category using laravel

I am trying to display subcategory to related category in this menu firsrt category has been displayed but i have to display subcatory to relate category ?

Database

category table

front page

controller

public function index(){
     $category=DB::table('category')->where('p_id',0)->get();
     return view('front_end/index',compact('category'));
}

html view

            <div class="nav-panel__nav-links nav-links">
            <ul class="nav-links__list">
            @foreach($category as $firstmenu)
               <li class="nav-links__item  nav-links__item--has-submenu ">
                <a id="cate" class="nav-links__item-link" href=" 
               {{$firstmenu->cate_name}}">
               <div class="nav-links__item-body">
               {{$firstmenu->cate_name}}
               <svg class="nav-links__item-arrow" width="9px" height="6px">
                <use xlink:href="public/assets/images/sprite.svg#arrow-rounded-down-9x6"></use>
                </svg>
                </div>
                </a>
                 <div class="nav-links__submenu nav-links__submenu--type--megamenu nav-links__submenu-- 
                 size--nl">
                   <!-- .megamenu -->
                   <div class="megamenu ">
                   <div class="megamenu__body">
                  <div class="row">
                     <div class="col-6">
                        <ul class="megamenu__links megamenu__links--level--0">
                           <li class="megamenu__item  megamenu__item--with-submenu ">
                                  /sub category /
                              <a href="">T-shirts</a>
                              <ul class="megamenu__links megamenu__links--level--1">
                                       /sub category /                               
                                 <li class="megamenu__item"><a href="">short paint</a>
                                 </li>
                              </ul>
                           </li>
                        </ul>
                     </div>
                  </div>
               </div>
            </div>
            <!-- .megamenu / end -->
         </div>
      </li>
      @endforeach
   </ul>
</div>

Upvotes: 1

Views: 2661

Answers (2)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

As you have posted the table structure

Your relationship will be like

  public function subCategories(){
        return $this->hasMany(SELF::class, 'p_id', 'id');
    }

    public function parentCategories(){
        return $this->hasMany(SELF::class, 'p_id', 'id');
    }

   public scopeParent($query){
       return $query->whereNull('p_id');
   }
   public scopeChild($query){
       return $query->whereNotNull('p_id');
   }

And you should be able to get the categories like

$categories = Category::parent()->get();

and then something like this

foreach($categories->with('subCategories')->get() as $category) {
   foreach($category->subCategories as $subCategory) {
    }
}

Upvotes: 1

Milena Grygier
Milena Grygier

Reputation: 396

so at first you need to fetch this subcategory somehow. You can use eloquent relationship if defined or simple join like so:

DB query:

$category = DB::table('category')                                              
           ->join('subcategory', 'subcategory.category_id', '=','category.id') 
           ->where('p_id',0)                                                
           ->get();

Eloquent(if category have more than one subcategory):

Category Model:

public function subcategory()
{
    return $this->hasMany(SubCategory::class, 'category_id', 'id');
}

Controller

$category = Category::with('subcategory')->get();

And then just get the name or some other value of subcategory in view (important - if there could be more subcategories related to your category than just one you have to use loop to itereate through all subcategories):

 {{$category->subcategory->name}}

Or

@foreach($category->subcategory as $sub_cat)
   {{$sub_cat->name}}
@endforeach

For more informations you should check Laravel documentation: https://laravel.com/docs/6.x/eloquent-relationships

Upvotes: 4

Related Questions