未定义变量:在每个列表项目商店的商店视图



这里是错误

![1]

= = = =

我很困惑如何让页面直接跳转到购物清单。这里的错误说明没有在循环中定义shop变量来显示已添加到购物车中的商品。但是数据已经输入到数据库

cart.blade.php

<div class="site-section">
<div class="container">
<div class="row mb-5">
<form class="col-md-12" method="post">
<div class="site-blocks-table">
<table class="table table-bordered">
<thead>
<tr>
<th class="product-thumbnail">Image</th>
<th class="product-name">Product</th>
<th class="product-price">Price</th>
<th class="product-quantity">Quantity</th>
<th class="product-total">Total</th>
</tr>
</thead>
<tbody>
@foreach( $shop as $customer )
<tr>
<th class ="row">{{ $loop->iteration }}</th>
<td>{{ $customer->image }}</td>
<td>{{ $customer->product }}</td>
<td>{{ $customer->price }}</td>
<td>{{ $customer->quantity }}</td>
<td>{{ $customer->total_price_prdt }}</td>
<td>
<a href="#" class="btn btn-primary btn-sm">X</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</form>
</div>
<div class="row">
<div class="col-md-6">
<div class="row mb-5">
<div class="col-md-6 mb-3 mb-md-0">
<button class="btn btn-primary btn-sm btn-block">Update Cart</button>
</div>
<div class="col-md-6">
<button class="btn btn-outline-primary btn-sm btn-block">Continue Shopping</button>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label class="text-black h4" for="coupon">Coupon</label>
<p>Enter your coupon code if you have one.</p>
</div>
<div class="col-md-8 mb-3 mb-md-0">
<input type="text" class="form-control py-3" id="coupon" placeholder="Coupon Code">
</div>
<div class="col-md-4">
<button class="btn btn-primary btn-sm">Apply Coupon</button>
</div>
</div>
</div>
<div class="col-md-6 pl-5">
<div class="row justify-content-end">
<div class="col-md-7">
<div class="row">
<div class="col-md-12 text-right border-bottom mb-5">
<h3 class="text-black h4 text-uppercase">Cart Totals</h3>
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<span class="text-black">Subtotal</span>
</div>
<div class="col-md-6 text-right">
<strong class="text-black"></strong>
</div>
</div>
<div class="row mb-5">
<div class="col-md-6">
<span class="text-black">Total</span>
</div>
<div class="col-md-6 text-right">
<strong class="text-black"></strong>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary btn-lg py-3 btn-block"
onclick="window.location='checkout.html'">Proceed To Checkout</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection

车控制部

<?php
namespace AppHttpControllers;
use Appaddcart;
use IlluminateSupportFacadesDB;
use Appcart;
use IlluminateHttpRequest;
use Auth;
use AppUser;
class cartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
// return view('customers/singleshop');
}
public function addToCart(Request $request)
{
// $product = DB::select('select * from add_cart where id='.$id);
// $user = Auth::user();
// $ac = new addcart();
// $ac->product = $request->product;
// $ac->product_price = $request->price;
// $ac->item_model = $request->model;
// $ac->quantity = $request->qty;
// $ac->save();
// // $shop_id = $ac->id;
// return redirect('/cart');
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$user = Auth::user();
$ac = new addcart();
$ac->product = 'tanktop';
// $ac->product = !empty($request->product) ? $request->product : 'default value';
$ac->product_price = 500.000;
$ac->item_model = $request->model;
$ac->quantity = $request->qty;
$ac->save();
// $shop_id = $ac->id;
return redirect('cart', $shop);
}
/**
* Display the specified resource.
*
* @param  Appcart  $cart
* @return IlluminateHttpResponse
*/
public function show(cart $cart)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param  Appcart  $cart
* @return IlluminateHttpResponse
*/
public function edit(cart $cart)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  Appcart  $cart
* @return IlluminateHttpResponse
*/
public function update(Request $request, cart $cart)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param  Appcart  $cart
* @return IlluminateHttpResponse
*/
public function destroy(cart $cart)
{
//
}
}
[1]: https://i.stack.imgur.com/3ukSj.png

您的购物车控制器将需要呈现视图或重定向到使用shop变量呈现视图的路由。我以为这是你的指数。由于没有定义索引,因此可以将shop变量添加到视图中,并按如下方式对其进行绑定。我相信你的商店是$ac变量。此外,根据您的后续评论,我假设您的刀片视图是/resources/views/customers.cart.blade.php。如果$ac不是您的产品列表,请将$ac替换为期望的列表。

public function store(Request $request)
{
$user = Auth::user();
$ac = new addcart();
$ac->product = 'tanktop';
// $ac->product = !empty($request->product) ? $request->product : 'default value';
$ac->product_price = 500.000;
$ac->item_model = $request->model;
$ac->quantity = $request->qty;
$ac->save();
// $shop_id = $ac->id;
return view('customers/cart',['shop'=>$ac]);
}

当你想要重定向到其他页面,你想要使用一个新的变量,你最让变量可用,你最使用compact, with方法。

我的建议是:


In your Controller/@store
return redirect()->route('Merchant view')->with( ['shop => $shop] );

It will work