删除购物车中的产品 laravel 7

时间:2021-04-26 06:25:16

标签: php laravel

我尝试使用 Laravel 开发购物车,我在此 link 中使用 Laravel 购物车,我的问题是我无法删除购物车中的产品,但会得到此 image

web.php

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

CartController.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>

1 个答案:

答案 0 :(得分:0)

使用此代码:

web.php

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

CartController.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>