我正在尝试将产品添加到购物车,当我单击“添加到购物车”,然后将其保存到购物车后,它必须重定向到购物车页面,产品被添加到购物车(保存到数据库购物车表中)但是购物车页面上显示:未定义的变量:购物车:(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
答案 0 :(得分:0)
在页面的其他位置使用$ cart变量的情况也可能是这种情况。如果您给出了整个cart.blade.php,这将很有帮助,因为您的@foreach也不太正确,您在结束标记之前结束了循环,而在开始之前没有打开循环...
@foreach($userCart as $cart)
<tr>
//your other elements consisting of $cart
</tr>
@endforeach
答案 1 :(得分:0)
我相信,如果您将购物车功能更改为以下功能,那么它将起作用。
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'));
}
答案 2 :(得分:0)
检查您的会话是否为空,并在不在控制器中的模型中进行查询 “面向对象编程laravel”。