Laravel 7 条件形式方法 POST |放
Laravel 7 conditional form method POST | PUT
您好,我正在使用 Laravel 7 并尝试根据以下条件提交:
if isset($category) == true
then the method should be PUT
if isset($category) == false
then method should change to POST
PUT 的更新方法在响应 302 从 POST 更改为 PUT 时工作正常。问题是,如果我要根据给定条件通过 POST 方法 提交,它会显示:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: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 |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
您需要调节 @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>
您好,我正在使用 Laravel 7 并尝试根据以下条件提交:
if
isset($category) == true
then the method should be PUT
ifisset($category) == false
then method should change to POST
PUT 的更新方法在响应 302 从 POST 更改为 PUT 时工作正常。问题是,如果我要根据给定条件通过 POST 方法 提交,它会显示:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: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 |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
您需要调节 @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>