我正在尝试显示与某个类别相关的产品。但是它显示了与其他类别也相关的所有产品。
刀片:
<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">
$75.00
</span>
</div>
</div>
</div>
@endforeach
@endforeach
</div>
控制器:
public function products(Request $request, Product $product)
{
$categories = Category::with('products')->distinct()->get();
return view('product.listing', compact('product', 'categories'));
}
路线:
Route::get('/product/{id}','Admin\ProductController@products')->name('product.products');
答案 0 :(得分:0)
public function products(Request $request, Category $category)
{
$products = Product::where('category_id',$category->id)->get();
$categories = Category:all();
return view('product.listing', compact('products','categories'));
}
答案 1 :(得分:0)
编辑控制器:
public function products(Request $request, Product $product)
{
$categories = $product->categories;
return view('product.listing', compact('product', 'categories'));
}
此外,您还应该在产品模型上具有类别关联方法。
答案 2 :(得分:0)
public function products(Request $request)
{
$categories = Category::where('id',$request->id)->with('products')->get();
return view('product.listing', compact('categories'));
}
@foreach($categories as $category)
@foreach ($category as $product)
答案 3 :(得分:0)
您的路线是
Route::get('/product/{id}','Admin\ProductController@products')->name('product.products');
//{id} it means you're getting category id from route right? so you can directly access it in controller.
控制器
//$id is from route.
public function products($id)
{
$products = Product::with('category')->where('category_id',$id)->get();
return view('product.listing', compact('products'));
}
在您的刀片文件
中@foreach($products as $product)
//here your all product which belongs to that categories.
and now if you want to access categories then may do as.
categories :- {{ $product->category->name }} //make sure it belongsTo in product
@endforech
答案 4 :(得分:0)
假设产品具有一个类别,并且在category_id
的{{1}}表更改products
方法中有一个products
列,如下所示。
ProductController
在您的public function products(Request $request, Product $product)
{
$relatedProducts = Product::where('category_id', $product->category_id)
->where('id', '!=', $product->id) // ignore current product
->get();
return view('product.listing', compact('product', 'relatedProducts'));
}
产品中替换id
。 Read More about route model binding.
route with
然后在您看来,您可以通过Route::get('/product/{product}','Admin\ProductController@products')->name('product.products');
进行迭代
答案 5 :(得分:0)
控制器:
public function products(Request $request, Category $category)
{
$category->load('products');
return view('product.listing', compact('category'));
}
路线:
Route::get('/product/{category}','Admin\ProductController@products')->name('product.products');
刀片文件:
@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>
</div>
</div>
@endforeach