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.
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>
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
Please or to participate in this conversation.