【问题标题】:The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. in laravel此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 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。出现

我该如何解决?请帮忙

【问题讨论】:

    标签: php laravel


    【解决方案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');//卖家删除产品我试试这个但是报错
      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 2019-12-29
      • 2020-05-27
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2020-04-19
      相关资源
      最近更新 更多