【发布时间】:2020-07-01 18:41:53
【问题描述】:
您好,我正在使用 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 |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+
【问题讨论】:
-
显示您的路线。
-
我正在使用资源:Route::resource('categories','CategoriesController');
-
你能试试
php artisan cache:clear和php artisan route:cache吗? -
完成运行这些命令。仍然有同样的错误。见这里:prnt.sc/rjqy1d
-
如果你手动添加这条路由会发生什么?
标签: php methods laravel-blade ternary laravel-7