【问题标题】:The GET method is not supported for this route. Supported: POST此路由不支持 GET 方法。支持:POST
【发布时间】:2020-09-04 06:38:40
【问题描述】:

我的表格:

 @if(!empty($Product) && !empty($ProductSpec))
            <form action="{{route('update_product')}}" method="POST">
                    <div class="form-group">
                        <label for="product_code">Product Code</label>
                        <input class="form-control" name="product_code" id="product_code" type="text" placeholder="product code..." value="{{$Product->product_code}}">
                    </div>
                    @error('product_code')
                    <div class="alert alert-danger">{{ $message }}</div>
                    @enderror

                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Update information</button>
                    </div>
                    {{ csrf_field() }}
                </form>
                @endif

路线:

Route::resource('add_product','ProductController');
Route::get('edit_product', 'ProductController@find_product_index')->name("edit_product");
Route::post('find_product', 'ProductController@find_product')->name("find_product");
Route::post('update_product', 'ProductController@update_product')->name("update_product");

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

控制器功能:

public function update_product(Request $request)
{
    $this->validate($request, [
        'name' => 'required|max:120|string']);
    return redirect()->route('edit_product')->with('success', 'Product Updated successfully');
}

我正在使用此代码并收到“此路由不支持 Get 方法,支持:POST”错误。 不知道我的代码有什么问题?起初我以为是控制器中的 MySQL 语句,但我删除了它们,我仍然遇到同样的问题。

编辑: php工匠路线:列表显示-> image

【问题讨论】:

  • php artisan route:list 说什么?
  • 检查文档....对于更新路由类型 PUT/PATCH... 您必须编辑表单以集成 @method('PUT');
  • laravel.com/docs/5.8/…。在此标题之前检查
  • 运行 php artisan route:clear 并重试
  • 我尝试清理路线,但没有帮助:(

标签: php laravel


【解决方案1】:

你的路线应该是:

Route::put('update_product/{update_product} ', 'ProductController@update_product')->name("update_product");

你的表单应该是这样的: ......

{{ csrf_field() }}
@method('PUT')
</form>

【讨论】:

  • 查看 add_product.update 路由,看看有什么不同。而且您可能应该在那里进行路由模型绑定。
  • 我切换到 add_product.update,但仍然遇到同样的错误
  • 我认为问题在于我在 1 页中有 2 个表单,第一个通过 ID 找到产品并将信息从我的数据库填充到第二个表单字段。我想我收到这个错误是因为有 2 种 POST 类型的表单,我猜这在 php/laravel 中是不允许的?
【解决方案2】:

运行php artisan optimize 以清除和重新配置您的缓存和路由。 清除您的路线是不够的。

【讨论】:

  • 我试过了,我在这里遇到了这个大错误:imgur.com/a/C9zXviS
  • 仅在 php artian config:cache 之后运行。您的错误意味着在您的 api 路由中,您有一条无法缓存的路由。你也可以进去注释掉,然后运行优化命令。
  • php artisan config:cache 工作,但没有修复错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2021-04-11
  • 2019-08-31
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多