Shawdow's avatar

Display particular subcategories products present in database

Hi,

ecommerce example where i have two table categories ans products in the categories table i have all the categories

in the product table i have all the products and also category_id as the foreign key which relates to the categories

the problem it display all the products name if i click the particular subcategory

example if i click the smartphone (subcategory) it should display the only samsung,sony products which relates to the smart phone but it displays the refrigirators products also like samsung,godrej(both smart phone and refrigirator)

any idea for solving the problem?

in the controller file

public function showsubcategory($name){
        
        $subcategory = \App\Product::where('name',$name)->first();
               
        $subcatdat = DB::table('categories')->select('id')->get();
        
        $product = DB::table('products')->select('*')->get();
        
        return \view ('subcategory',compact(' product','subcatdat'));
    }

in the blade file

 @foreach($product as $products)
     @foreach($subcatdat as $subcatdatas)   
    @if($subcatdatas->id == $products->category_id)
     
  <p> {{ $products->name }}</p>
    @endif
      @endforeach
   
   @endforeach

0 likes
0 replies

Please or to participate in this conversation.