【问题标题】:Laravel-5.7: Undefined variable: cartLaravel-5.7:未定义变量:购物车
【发布时间】:2019-01-28 14:55:06
【问题描述】:

我正在尝试将产品添加到购物车,当我单击添加到购物车然后将其保存到购物车后它必须重定向到购物车页面,产品正在添加到购物车(保存到 db 购物车表)但购物车页面显示:未定义变量:购物车 (View:D:\xampp\htdocs\laravel\ecom2\resources\views\products\cart.blade.php)

ProductsController代码:

  public function addtocart(Request $request){
  $data = $request->all();
  //echo "<pre>"; print_r($data); die;

  if(empty($data['user_email'])){
    $data['user_email'] = '';
  }


  $session_id = Session::get('session_id');
  if(empty($session_id)){
      $session_id = str_random(40);
      Session::put('session_id',$session_id);
  }


  DB::table('cart')->insert(['product_id'=>$data['product_id'],'product_name'=>$data['product_name'],'product_code'=>$data['product_code'],'product_color'=>$data['product_color'],'price'=>$data['price'],'size'=>$data['size'],'quantity'=>$data['quantity'],'user_email'=>$data['user_email'],'session_id'=>$session_id]);

  return redirect('cart')->with('flash_message_success','Product has been added in Cart!');
}

public function cart(){
  $session_id = Session::get('session_id');
  $userCart = DB::table('cart')->where(['session_id'=>$session_id])->get();
  foreach ($userCart as $key => $product) {
    $productDetails = Product::where('id',$product->product_id)->first();
    $userCart[$key]->image = $productDetails->image;
  }
  return view('products.cart')->with(compact('userCart'));
}

cart.blade.php:

  @extends('layouts.frontLayout.front_design')
  @section('content')

 <!-- Cart -->
 <section class="cart bgwhite p-t-70 p-b-100">
    <div class="container">
        <!-- Cart item -->
        <div class="container-table-cart pos-relative">
            <div class="wrap-table-shopping-cart bgwhite">
                <table class="table-shopping-cart">
                    <tr class="table-head">
                        <th class="column-1"></th>
                        <th class="column-2">Product</th>
                        <th class="column-3">Price</th>
                        <th class="column-4 p-l-70">Quantity</th>
                        <th class="column-5">Total</th>
                    </tr>

                    <tr class="table-row">
                        <td class="column-1">
                            <div class="cart-img-product b-rad-4 o-f-hidden">
                                <img src="{{ 
    asset('images/backend_images/products/small/'.$cart->image) }}" alt="IMG- 
     PRODUCT">
                            </div>
                        </td>
                        @foreach($userCart as $cart)
                        <td class="column-2">{{ $cart->product_name }}</td>
                        <td class="column-3">PKR: {{ $cart->price }}</td>
                        <td class="column-4">
                            <div class="flex-w bo5 of-hidden w-size17">
                                <button class="btn-num-product-down color1 flex-c-m size7 bg8 eff2">
                                    <i class="fs-12 fa fa-minus" aria-hidden="true"></i>
                                </button>

                                <input class="size8 m-text18 t-center num-product" type="number" name="quantity" value="{{ $cart->price }}">

                                <button class="btn-num-product-up color1 flex-c-m size7 bg8 eff2">
                                    <i class="fs-12 fa fa-plus" aria-hidden="true"></i>
                                </button>
                            </div>
                        </td>
                        <td class="column-5">PKR: {{ $cart->price*$cart->quantity }}</td>
                    </tr>
                        @endforeach
                </table>
            </div>
        </div>

        <div class="flex-w flex-sb-m p-t-25 p-b-25 bo8 p-l-35 p-r-60 p-lr-15-sm">
            <div class="flex-w flex-m w-full-sm">
                <div class="size11 bo4 m-r-10">
                    <input class="sizefull s-text7 p-l-22 p-r-22" type="text" name="coupon-code" placeholder="Coupon Code">
                </div>

                <div class="size12 trans-0-4 m-t-10 m-b-10 m-r-10">
                    <!-- Button -->
                    <button class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4">
                        Apply coupon
                    </button>
                </div>
            </div>

            <div class="size10 trans-0-4 m-t-10 m-b-10">
                <!-- Button -->
                <button class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4">
                    Update Cart
                </button>
            </div>
        </div>

        <!-- Total -->
        <div class="bo9 w-size18 p-l-40 p-r-40 p-t-30 p-b-38 m-t-30 m-r-0 m-l-auto p-lr-15-sm">
            <h5 class="m-text20 p-b-24">
                Cart Totals
            </h5>

            <!--  -->
            <div class="flex-w flex-sb-m p-b-12">
                <span class="s-text18 w-size19 w-full-sm">
                    Subtotal:
                </span>

                <span class="m-text21 w-size20 w-full-sm">
                    $39.00
                </span>
            </div>

            <!--  -->
            <div class="flex-w flex-sb bo10 p-t-15 p-b-20">
                <span class="s-text18 w-size19 w-full-sm">
                    Shipping:
                </span>

                <div class="w-size20 w-full-sm">
                    <p class="s-text8 p-b-23">
                        There are no shipping methods available. Please double check your address, or contact us if you need any help.
                    </p>

                    <span class="s-text19">
                        Calculate Shipping
                    </span>

                    <div class="rs2-select2 rs3-select2 rs4-select2 bo4 of-hidden w-size21 m-t-8 m-b-12">
                        <select class="selection-2" name="country">
                            <option>Select a country...</option>
                            <option>US</option>
                            <option>UK</option>
                            <option>Japan</option>
                        </select>
                    </div>

                    <div class="size13 bo4 m-b-12">
                    <input class="sizefull s-text7 p-l-15 p-r-15" type="text" name="state" placeholder="State /  country">
                    </div>

                    <div class="size13 bo4 m-b-22">
                        <input class="sizefull s-text7 p-l-15 p-r-15" type="text" name="postcode" placeholder="Postcode / Zip">
                    </div>

                    <div class="size14 trans-0-4 m-b-10">
                        <!-- Button -->
                        <button class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4">
                            Update Totals
                        </button>
                    </div>
                </div>
            </div>

            <!--  -->
            <div class="flex-w flex-sb-m p-t-26 p-b-30">
                <span class="m-text22 w-size19 w-full-sm">
                    Total:
                </span>

                  <span class="m-text21 w-size20 w-full-sm">
                    $39.00
                  </span>
              </div>

              <div class="size15 trans-0-4">
                <!-- Button -->
                <button class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4">
                    Proceed to Checkout
                </button>
            </div>
          </div>
       </div>
    </section>

   @endsection

【问题讨论】:

  • 显示 cart.blade.php 页面
  • 您将 userCart 传递给刀片或查看这就是原因。您收到此错误
  • 最初它工作正常,然后我在添加会话后添加 session_id init 显示此错误。但 session_id 也保存在数据库中
  • @NabilFarhan cart.blabe.php 页面现在有问题
  • 你在刀片中有 $userCart,你迭代它并创建你的 $cart 但在你使用 $cart->image 的 foreach 之上,只有 $cart 是未定义的。所以你需要将 foreach 移到它上面

标签: laravel eloquent laravel-5.7


【解决方案1】:

这也可能是您在页面的其他地方使用了 $cart 变量的情况。如果您提供整个 cart.blade.php 会很有帮助,因为您的 @foreach 也不完全正确,您已经在结束标记之前结束了循环,并且在开始之前没有打开循环......

    @foreach($userCart as $cart)
     <tr>
      //your other elements consisting of $cart
     </tr>
    @endforeach

【讨论】:

  • 完整的购物车文件有问题
  • 这是您的问题。 asset('images/backend_images/products/small/'.$cart->image) }}" alt="IMG- PRODUCT"> 你已经使用了 $cart->image,但是你没有发送 $cart 变量,你已发送 $userCart。要么这应该在你的循环中,要么你可能有一些其他图像要显示在存储在其他变量中的特定部分并在那里使用它。
【解决方案2】:

我相信如果您将购物车功能更改为以下内容,它将起作用。

public function cart(){
  $session_id = Session::get('session_id');
  $userCart = DB::table('cart')->where('session_id', $session_id)->get();
  foreach ($userCart as $key => $product) {
    $productDetails = Product::find($product->product_id);
    $product->image = $productDetails->image;
  }
  return view('products.cart', compact('userCart'));
}

【讨论】:

  • 现在检查@programmer0001
  • } 返回视图('products.cart, compact('userCart')); } 这行有语法错误
  • 嗯,是的,我忘了一个逗号。现在检查。 @programmer0001
  • 请接受此答案,以便将其关闭。 @programmer0001
【解决方案3】:

检查您的会话是否为空,并在不在控制器中的模型中进行查询 “面向对象编程 laravel”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2019-03-01
    • 2019-06-20
    • 1970-01-01
    • 2019-09-28
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多