您好我正在使用 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 |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+