gpt4 book ai didi

php - 在laravel项目上未定义路线[购物车]

转载 作者:行者123 更新时间:2023-12-03 08:39:49 26 4
gpt4 key购买 nike

在我的laravel购物车上,我创建了一个delete函数,使用json或dd函数进行测试时似乎还可以,但是尝试返回路线购物车 View Route [cart] not defined.时,这是一个问题。当我只想删除同一产品的1个项目时,还需要优化该功能,这意味着每行需要“删除1个项目”和“删除所有项目”。
购物车型号:

class Cart 
{
public $items = null;
public $totalQty = 0;
public $totalPrice = 0;

public function __construct($oldCart)
{
if ($oldCart) {
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}

public function add($item, $id)
{
$storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item, 'imagePath' => $item->imagePath];

if ($this->items ) {

if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$storedItem['imagePath'] = $item->imagePath;
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}

public function delete($id) {

if(!$this->items || !isset($this->items[$id])) {
return false; // maybe throw an exception here?
}
data of $this->items, create a method to calculate the totals when needed.
$this->totalQty -= $this->items[$id]['qty'];
$this->totalPrice -= $this->items[$id]['price'] * $this->items[$id]['qty'];

// and remove the item
unset($this->items[$id]);
}
}
推车 Controller :
class ProductController extends Controller

{
/**
*@return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('home', ['products'=> $products]);
}

public function getAddToCart(Request $request, $id)
{
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);

$request->session()->put('cart', $cart);
return redirect()->route('home');
}

public function getCart()
{
if (!Session::has('cart')) {
return view('cart');
}
$oldCart = Session::get('cart');

$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}

/**
* Remove the specified resource from storage.
* @param int $id
* @return \Illuminate\Http\Request
*/
public function destroy(Request $request, $id) {
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->delete($id);
$request->session()->put('cart', $cart);
return redirect()->route('cart');


//return response()->json(array( 'totalqty' => $cart->totalQty, 'totalPrice' => $cart->totalPrice));
//{"totalqty":0,"totalPrice":-222000}
}

}
购物车 View :
<form method="POST" action="{{ route('cart.destroy', ['id' => $product['item']['id']]) }}" > 
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete all items" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
<input type="submit" value="Delete 1 item" onclick="return confirm('Are you sure?')" class="btn btn-danger" />
</form>

最佳答案

我解决了从return redirect()->route('cart');return redirect()->route('shoppingCart');的路线购物车的问题。

关于php - 在laravel项目上未定义路线[购物车],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62707531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com