Laravel 7条件表格方法POST | PUT

问题描述 投票:0回答:1

嗨,我正在使用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          |
+--------+-----------+----------------------------+--------------------+------------------------------------------------------------------------+--------------+

[嗨,我正在使用Laravel 7,并尝试根据以下条件提交:如果isset($ category)== true,则该方法应为PUT;如果isset($ category)== false,则该方法应更改为.. 。

php methods laravel-blade ternary laravel-7
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>
© www.soinside.com 2019 - 2024. All rights reserved.