0

您好我正在使用 Laravel 7 并尝试根据以下条件提交:

如果isset($category) == true则该方法应为PUT
如果isset($category) == false则该方法应更改为POST

更新方法是PUT工作正常,响应302 从 POST 更改为 PUT。问题是如果我要根据给定条件通过POST 方法提交,它会说:

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

任何建议如何做到这一点,以相同的形式?

这是我的代码:

<form action="{{ isset($category) ? route('categories.update',$category->id) : route('categories.store') }}" method="POST">
    @csrf
    @method('PUT')
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" id="name" class="form-control" name="name" value="{{ isset($category) ? $category->name : '' }}">
        </div>

   <div class="form-group">
      <button class="btn btn-success">
        {{ isset($category) ? 'Update category' : 'Add category' }}
      </button>
   </div>
</form>

我正在使用Route::resource('categories','CategoriesController');

+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                        | Name               | Action                                                                 | Middleware   |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
|        | POST      | categories                 | categories.store   | App\Http\Controllers\CategoriesController@store                        | web          |
|        | GET|HEAD  | categories                 | categories.index   | App\Http\Controllers\CategoriesController@index                        | web          |
|        | GET|HEAD  | categories/create          | categories.create  | App\Http\Controllers\CategoriesController@create                       | web          |
|        | DELETE    | categories/{category}      | categories.destroy | App\Http\Controllers\CategoriesController@destroy                      | web          |
|        | PUT|PATCH | categories/{category}      | categories.update  | App\Http\Controllers\CategoriesController@update                       | web          |
|        | GET|HEAD  | categories/{category}      | categories.show    | App\Http\Controllers\CategoriesController@show                         | web          |
|        | GET|HEAD  | categories/{category}/edit | categories.edit    | App\Http\Controllers\CategoriesController@edit                         | web          |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
4

1 回答 1

1

您需要@method('PUT')像下面这样调节:

<form action="{{ isset($category) ? route('categories.update',$category->id) : route('categories.store') }}" method="POST">
    @csrf
    @if(isset($category))
       @method('PUT')
    @endif
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" id="name" class="form-control" name="name" value="{{ isset($category) ? $category->name : '' }}">
        </div>

   <div class="form-group">
      <button class="btn btn-success">
        {{ isset($category) ? 'Update category' : 'Add category' }}
      </button>
   </div>
</form>
于 2020-03-21T07:22:24.147 回答