Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JOHNMAC's avatar

all products are showing whereas to show only one category related products

hi m trying to show products related to that category but it is showing all products which are also related to other categories,,, url shown when I click on category and go on product listing page is http://127.0.0.1:8000/product/6 but at bottom all products are shown which are also related to other categories

blade file:

  <div class="row">
                @foreach($categories as $category)
                    @foreach ($category->products as $product)
                <div class="col-sm-12 col-md-6 col-lg-4 p-b-50">
                    <!-- Block2 -->
                    <div class="block2">
                        <div class="block2-img wrap-pic-w of-hidden pos-relative block2-labelnew">
                            <img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $product->product_image }}" class="img-thumbnail" style="width: 270px; height: 360px;" />

                            <div class="block2-overlay trans-0-4">

                            </div>
                        </div>

                        <div class="block2-txt p-t-20">
                            <a href="product-detail.html" class="block2-name dis-block s-text3 p-b-5">
                                {{ $product->product_name }}
                            </a>

                            <span class="block2-price m-text6 p-r-5">
                                .00
                            </span>
                        </div>
                    </div>
                </div>
                    @endforeach
                @endforeach
            </div>

controller:

   public function products(Request $request, Product $product)
   {
      $categories = Category::with('products')->distinct()->get();
      return view('product.listing', compact('product', 'categories'));
  }

route:

  Route::get('/product/{id}','Admin\ProductController@products')->name('product.products');

i am coming to product listing page from index page by clicking on caregory: (code of that link)

  <a href="{{ route('product.products', $category->id) }}" class="flex-c-m size2 m-text2 bg3 hov1 trans-0-4"> {{ ucwords($category->category_name) }} </a>
0 likes
8 replies
LoganCodes's avatar

It looks like you are passing a separate $product variable from your controller which blade is picking up on. Try removing the $product variable from the compact() method and try again.

1 like
JOHNMAC's avatar

now controller is

  public function products(Request $request)
  {
    $categories = Category::with('products')->distinct()->get();
    return view('product.listing', compact('categories'));
  }

but still same issue and if I remove it from blade then it says:

  Undefined offset: 1
LoganCodes's avatar

You still need to leave $product in the method because the route is looking for it.

public function products(Request $request, $product)
{
    // code goes here
}

See if that works

Snapey's avatar
Snapey
Best Answer
Level 122

but you are not doing anything to only get the one category?

You pass category->id into the controller. Use it.

  public function products(Request $request, $id)
  {
    $category = Category::with('products')->find($id);
    return view('product.listing', compact('category'));
  }

and use $category in the view and don't loop over categories

Snapey's avatar

or route model bind

Route;

  Route::get('/product/{category}','Admin\ProductController@products')->name('product.products');

(however this is a messed up route - this should be for a single product)

Controller

  public function products(Request $request, Category, $category)
  {
    $category->load('products');
    return view('product.listing', compact('category'));
  }
1 like
Snapey's avatar

both examples are eager loading

1 like

Please or to participate in this conversation.