i have littel problem in my code, i dont know write mastake but i check my code is good
This ShopController `
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show');
}
`
this my route
`
Route::get('/shop/detail/{id}', 'ShopController@show');
`
this my view
`
<div >
<h2 >{{$product->name}}</h2>
<hr>
<div >
<div >
<div id="picture">
<img src="{{asset($product->image)}}" alt="" height="200" width="200">
</div>
</div>
<div >
<h4 id="description">Description</h4>
<p>{{$product->desc}}</p>
</div>
<div >
<div >
<p>Harga</p>
<h2>Rp {{number_format($product->price)}}</h2>
<form action="" method="POST">
@csrf
<input type="hidden" value="" name="item_id">
<input type="submit" value="Add to Cart">
</form>
</div>
</div>
</div>
</div>
`
I have checked my code and there are no errors
CodePudding user response:
You don't pass the product to the view. You need to compact the variable like this in the controller:
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show', compact('product'));
}
CodePudding user response:
you are getting product from database in $product variable, but not passing that variable to your view. There are several ways to pass variables to view.
return view('shop.show', compact('product'));
OR
return view('shop.show', ['product' => $product]);
OR
return view('shop.show', get_defined_vars());
get_defined_vars() is built-in php function, by using this function any numbers of variables declared in method, all will be passed to view