laravel 此路由不支持该方法,支持的方法:GET、HEAD、POST

xoshrz7s  于 2023-10-22  发布在  其他
关注(0)|答案(6)|浏览(175)

我正在创建一个显示一些数据的索引表单。一切都准备好了,但当我删除按钮,我得到一个错误“该方法不支持此路由。支持的方法:GET,HEAD,POST.”

路线

  1. Route::group(['middleware' => ['auth']], function() {
  2. Route::resource('roles','RoleController');
  3. Route::resource('users','UserController');
  4. Route::resource('kamar_theresia','Kamar_TheresiaController');
  5. });

控制器

  1. public function destroy($id)
  2. {
  3. Kamar_Theresia::find($id)->delete();
  4. return redirect()->route('kamar_theresia.index')
  5. ->with('success','Kamar Theresia deleted successfully');
  6. }

查看

  1. @foreach ($kamar_theresia as $tere)
  2. <tr>
  3. <td>{{ ++$i }}</td>
  4. <td>{{ $tere->nama }}</td>
  5. <td>{{ $tere->name }}</td>
  6. <td>{{ $tere->ketersediaan }}</td>
  7. <td>
  8. @can('theresia-delete')
  9. {!! Form::open(['method' => 'DELETE','route' => ['kamar_theresia.destroy', $tere->id],'style'=>'display:inline']) !!}
  10. {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
  11. {!! Form::close() !!}
  12. @endcan
  13. </td>
  14. </tr>
  15. @endforeach
yizd12fk

yizd12fk1#

确保您的表单没有包含在另一个表单中。我犯了这个愚蠢的错误,得到了同样的错误消息。

z2acfund

z2acfund2#

这是因为你传递了POST方法作为你的表单的方法,这是错误的,正确的做法是传递POST方法。

请看这个例子:

  1. <form action="{{ route('kamar_theresia.destroy', $tere->id) }}" method="POST">
  2. @csrf
  3. @method('delete')
  4. <button type="submit" class="btn btn-outline-danger">Delete</button>
  5. </form>

您的控制器应该是:

  1. public function destroy(Kamar_Theresia $khamar_teresia)
  2. {
  3. $khamar_teresia->delete();
  4. return redirect()->route('kamar_theresia.index')
  5. ->with('success','Kamar Theresia deleted successfully');
  6. }
展开查看全部
mdfafbf1

mdfafbf13#

看起来你快到了!我会使用POST的形式类似于这样:

  1. {{ Form::open(['method' => 'POST', 'route' => ['kamar_theresia.destroy']) }}
  2. {{ Form::hidden('id',$tere->id) }}
  3. {{ Form::submit('Delete') }}
  4. {{ Form::close() }}

然后在你的控制器里

  1. public function destroy(Request $request){
  2. $id = $request->input('id');
  3. Kamar_Theresia::find($id)->delete();

剩下的代码应该没问题。如果不行就告诉我。

qv7cva1a

qv7cva1a4#

在Form中使用{{ csrf_field() }}{{ method_field('DELETE') }}

  1. {{ csrf_field() }}
  2. {{ method_field('DELETE') }}

在Controller中使用此

  1. public function destroy($id)
  2. {
  3. $delete = kamar_theresia::find($id);
  4. $delete->delete();
  5. return redirect('/')->with('deleted','Kamar Theresia deleted successfully');
  6. }

如果我们使用的是Route::resource(),那么它将通过destroy函数自动路由。

hmmo2u0o

hmmo2u0o5#

忘记在开始时使用slash:

  1. <form method="POST" action={{--here=> --}}"/save_edit_delete_order/{{$order_id}}">
  2. @csrf
  3. @method('delete')
  4. ......
  5. <div class="modal-footer">
  6. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  7. <button type="submit" class="btn btn-primary">Yes, I am</button>
  8. </div>
  9. </div>
  10. </form>

在资源控制器中:

  1. public function destroy($id)
  2. {
  3. return 'kuku';
  4. }
展开查看全部
cuxqih21

cuxqih216#

查看

  1. <form action="{{route('command.delete',[$command->id,$command->car_id])}}" method="post">
  2. @csrf
  3. {{method_field('delete')}}
  4. <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
  5. </form>

网站

  1. Route::delete('/commands/{commandId}/{carId}/delete','CommandController@deleteUserCommands')->name('command.delete');

相关问题