此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 laravel 中

Posted

技术标签:

【中文标题】此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 laravel 中【英文标题】:The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. in laravel 【发布时间】:2020-02-16 06:31:29 【问题描述】:

我想做卖家可以编辑和更新产品

这是产品控制器

public function edit($id)
    
        $product = Product::find($id);
        return view('product.edit', compact('product'));
    

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    
        $product = Product::find($id);
        $product-> title = $request-> title;
        $product-> description = $request-> description;
        $product-> price = $request-> price;

        if($request->hasFile('image'))
            $file = $request-> file('image');
            $filename = time().'.'.$file-> getClientOriginalExtension();
            $location = public_path('/images');
            $file-> move($location, $filename);
            $oldImage = $product->image;
            \Storage::delete($oldImage);
            $product-> image= $filename;
        
        $product-> save();

        return back();
    

这是edit.blade.php

<form action="route('product.update', $product->id)" method="post" enctype="multipart/form-data">
        csrf_field()
        method_field('put') 
[...]

<button type="submit" class="btn btn-success">Submit</button>

这是 web.php

Route::get('/index', 'ProductController@index');//seller view all product

Route::get('/create', 'ProductController@create'); //seller create new product
Route::post('','ProductController@store')->name('product.store'); //store in database

Route::get('/edit/id','ProductController@edit'); // seller edit post
Route::post('','ProductController@update')->name('product.update'); //seller update

当我点击提交按钮进行更新时 此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。出现

我该如何解决?请帮忙

【问题讨论】:

【参考方案1】:

你应该在路由中使用 PUT;

Route::put('','ProductController@update')->name('product.update');

而不是 produc.update 不是除了 product->id

<form action="route('product.update')" method="post" enctype="multipart/form-data">
        csrf_field()
        method_field('put') 
[...]

<button type="submit" class="btn btn-success">Submit</button>

【讨论】:

函数 App\Http\Controllers\ProductController::update() 的参数太少,通过了 1 个,预期正好有 2 个 @user12149659 只需将表单操作路由更改为&lt;form action="route('product.update', $product-&gt;id)【参考方案2】:

您需要在表单请求中传递id 参数,如下所示:

<form action=" route('product.update', $product->id) " method="post" enctype="multipart/form-data">
 csrf_field() 
 method_field('put') 
[...]

<button type="submit" class="btn btn-success">Submit</button>

然后修改你的控制器方法如下:

Route::put('edit/id','ProductController@update')->name('product.update');

这是因为您的控制器方法期望在请求中传递 id,但实际上并没有收到,因此出现错误。

我希望这会有所帮助!

【讨论】:

【参考方案3】:

请再试一次;

Route::put('edit/id','ProductController@update')->name('product.update');

<form action=" route('product.update', ["id" => $product->id]) " method="post" enctype="multipart/form-data">
 csrf_field() 
 method_field('put') 
[...]

<button type="submit" class="btn btn-success">Submit</button>

【讨论】:

请更新您之前的答案,如果它不能解决问题 如果卖家想删除产品我该怎么做
$product->id]) " method="post"> csrf_field() method_field('DELETE')
Route::delete('','ProductController@destroy')->name('product.destroy');//卖家删除产品我试试这个但是报错

以上是关于此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 laravel 中的主要内容,如果未能解决你的问题,请参考以下文章

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 GET 方法。支持的方法:PUT

此路由不支持 GET 方法。支持的方法:PUT。与放置形式。拉拉维尔

此路由不支持 PUT 方法。 Vue/laravel

Laravel 6.0 不支持 DELETE、PUT 方法

此路由不支持 GET 方法。支持的方法:POST。在插入/存储

此路由 Laravel + ConfirmBox 不支持 GET 方法