首页 > 解决方案 > 删除购物车 laravel 7 中的产品

问题描述

我尝试使用 laravel 开发购物车,我在此链接中使用 laravel 购物车我的问题是我无法删除购物车中的产品,并且会得到这张图片

网页.php

Route::post('/cart/{rowId}', 'CartController@destroy')->name('cart.destroy');

购物车控制器.php

public function destroy($id)
{
    \Cart::remove($id);
    return back()->with('success', 'Item has been removed!');
}

index.blade.php

<form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">
    <button class="btn btn-sm btn-danger">Remove</button>
</form>

标签: phplaravel

解决方案


使用此代码:

网页.php

Route::get('/cart/{rowId}', 'CartController@destroy')->name('cart.destroy');

购物车控制器.php

public function destroy($id) {
    $cart = Cart::findOrFail($id);
    $cart->delete();

    return redirect()->route('')->with('success', 'Item has been removed!');
}

index.blade.php

<a href="{{ route('cart.destroy', $item->rowId) }}" class="" title="Remove Cart">
    <i class="fa fa-trash"></i>
</a>

推荐阅读