我是Laravel的新手,并使用laravel购物车library和Laravelcollective创建了一个ecomm网站。我目前停留在单个删除按钮上的Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException但是如果我们创建另一个删除按钮,则第二个按钮可以正常使用相同的代码,而第一个按钮会创建错误。请帮我解决这个错误。
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/','FrontController@index')->name('index');
Route::get('shop', 'FrontController@shop')->name('shop');
Route::get('details', 'FrontController@details')->name('productdetails');
Route::get('/logout', 'Auth\LoginController@logout');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('/cart','CartController');
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
Route::get('/', 'AdminController@index')->name('admin.index');
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
});
我的错误创建页面为here。
顶部的表单标记创建错误,底部的表单标记正常工作。
我的控制器是:
<?php
namespace App\Http\Controllers;
use Gloudemans\Shoppingcart\Facades\Cart;
use App\Product;
use Illuminate\Http\Request;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products=Product::orderBy('id','DESC')->take(4)->get();
$cartItems = Cart::content();
return view('cart.index',compact('products','cartItems'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = Product::find($id);
Cart::add($id,$product->name,1,$product->price);
return back();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
Cart::update($id,$request->qty);
return back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Cart::remove($id);
return back();
}
}
我只希望顶层表单标记起作用。请指导我。感谢
答案 0 :(得分:0)
因为您正在为购物车使用资源控制器。所以你应该使用delete方法删除按钮。
<form action="{{route('cart.destroy',$cartItem->rowId)}}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button>Delete</button>
</form>
答案 1 :(得分:0)
您可以尝试
Route::resource('/cart','CartController')->name('cart');