2017-06-30 118 views
1

我收到一個未定義的變量錯誤,我不知道爲什麼。這裏是我的代碼:未定義變量:product_cart

@if(Session::has('cart')) 
             @foreach($product_cart as $cart) 
             <!-- one item --> 
             <div class="media"> 
              <img width="25%" src="source/image/product/{{$cart['item']['image']}}" alt="" 
     class="pull-left"> 
              <div class="media-body"> 
               <p class="font-large">{{$cart['item']['name']}}</p> 
               <span class="color-gray your-order-info">Đơn giá: {{number_format($cart['price'])}} đồng</span> 
               <span class="color-gray your-order-info">Số lượng: {{$cart['qty']}}</span> 
              </div> 
             </div> 
             <!-- end one item --> 
             @endforeach 
@endif 
+0

從哪裏獲取數組$ product_cart中的數據? – Nivedita

+0

你通過$ product_cart的地方? –

+0

請顯示您的控制器部件以查看此刀片文件 –

回答

1

我不知道,但laravel我假設$product_cart應該在車的所有產品。

所以我不能找到你分配會話變量到$product_cart的部分,然後在foreach中使用它。

0
@if(Session::has('cart')) 
    @foreach(session::get('cart') as $cart) 
    <!-- one item --> 
    <div class="media"> 
     <img width="25%" src="source/image/product/{{$cart['item']['image']}}" alt="" class="pull-left"> 
     <div class="media-body"> 
      <p class="font-large">{{$cart['item']['name']}}</p> 
      <span class="color-gray your-order-info">Đơn giá: {{number_format($cart['price'])}} đồng</span> 
      <span class="color-gray your-order-info">Số lượng: {{$cart['qty']}}</span> 
     </div> 
    </div> 
    <!-- end one item --> 
    @endforeach 
@endif 
+0

@start_nay您需要改進您的帖子 – Emiliano

+1

您需要說明您爲什麼做得更好或爲什麼用戶做錯了 – Emiliano

+0

也許產品購物車未通過控制器查看。例如,當該控制器的方法調用它的方法和方法返回視圖,如該視圖('cart-content')或包含其他視圖像@include('cart-content')時,包含的形式就像這樣@include('cart -content」,[ 'product_cart'=> $ product_cart]) –

0

我看到如果你沒有從視圖傳遞那麼變量product_cart它沒有被賦值。所以這樣做更好;

@if(Session::has('cart')) 
@foreach(Session::has('cart') as $cart) 
<!-- one item --> 
<div class="media"> 
    <img width="25%" src="source/image/product/{{$cart['item']['image']}}" alt="" 
     class="pull-left"> 
    <div class="media-body"> 
     <p class="font-large">{{$cart['item']['name']}}</p> 
     <span class="color-gray your-order-info">Đơn giá: {{number_format($cart['price'])}} đồng</span> 
     <span class="color-gray your-order-info">Số lượng: {{$cart['qty']}}</span> 
    </div> 
</div> 
<!-- end one item --> 
@endforeach 
@endif