skip to Main Content

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 class="container">
  <h2 class="title">{{$product->name}}</h2>
  <hr>
  <div class="row">
    <div class="wrapper">
      <div class="col-lg-4" id="picture">
      <img src="{{asset($product->image)}}" alt="" height="200" width="200">
      </div>
    </div>
    <div class="col-lg-4 desc">
      <h4 id="description">Description</h4>
      <p>{{$product->desc}}</p>
    </div>
    <div class="col-lg-4">
      <div class="kartu">
        <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" class="btn btn-primary" value="Add to Cart">
    </form>
      </div>
    </div>
  </div>
</div>

`

I have checked my code and there are no errors

2

Answers


  1. 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'));
    }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search