我在單一產品頁面上顯示一個產品時出現問題。因此,如果我現在在首頁單擊產品,我想查看特定產品。按單個產品頁面上的ID顯示產品
現在我看到所有的產品,而不是我點擊的產品。
這是路線:
Route::get('/product/{id}', [
'uses' => '[email protected]',
'as' => 'product.single'
]);
這是控制器:
public function getProduct($id)
{
$product = Product::find($id);
return view('pages.product', ['id' => $product->id]);
}
這是視圖(product.blade.php):
@foreach(App\Product::all() as $product)
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src= {{$product->imagePath}} alt="...">
<div class="caption">
<h3> {{$product->title}} </h3>
<p class="discription"> {{$product->discription}} </p>
<div class="clearfix">
<div class="pull-left price"/>$ {{$product->price}}</div>
<a href= {{ route('product.addToCart', ['id' => $product->id ]) }} class="btn btn-danger pull-right" role="button">Add to cart</a>
</div>
</div>
</div>
@endforeach
這是錯誤的在許多層面上。你怎麼能寫'@foreach(App \ Product :: all()as $ product)'並期望它顯示單個產品?投票關閉太寬 – Steve
我只是在學習的人,如果你沒有幫助我,你不必回答 – Mette
我不想粗魯。我只是不認爲你是通過提問這樣的問題來學習任何東西。您應該研究基礎知識(變量,控制結構),而不是複雜的mvc框架。 – Steve