controller.php
$shop['product'] = Product::with('ProductImages')->where('product_status','=',1)->where('product_drop_status','=','no')->where('language_code','=',$translate)->orderBy('product_id','desc')->get();
index.blade.php
@foreach($categories['display'] as $category)
<div class="container pt-3 mt-3 pb-3 mb-3">
<div class="row">
<h4 class="black mb-2 pb-2">{{ $category->category_name }} {{ $category->cat_id }}</h4>
<div class="swiper-container">
<div class="swiper-wrapper"> <!-- ok -->
@php $z = 1; @endphp
@foreach($shop['product'] as $product)
<!-- display products -->
@endfoeeach
i want to show category wise products.but it shows all product
what should i try??
My Answer:
To show category wise products in Laravel, you can modify your controller and view files as follows:
Controller.php:
Modify your controller to fetch products based on the category ID. Assuming you have a relationship between categories and products, you can use the category ID to filter products.
For example:
php
$categories = Category::all();
$products = [];
foreach($categories as $category) {
$products[$category->id] = Product::with('ProductImages')
->where('category_id', $category->id)
->where('product_status', 1)
->where('product_drop_status', 'no')
->where('language_code', $translate)
->orderBy('product_id', 'desc')
->get();
}
return view('index', compact('categories', 'products'));
Index.blade.php:
Modify your view file to loop through the categories and display products for each category.
For example:
html
@foreach($categories as $category)
{{ $category->category_name }}
@foreach($products[$category->id] as $product)
@endforeach
@endforeach
This way, you will be able to display products category wise on your index page. Make sure to adjust the code according to your database structure and relationships.
Rate this post
3 of 5 based on 1495 votesComments