【问题标题】:put method not supported in laravellaravel 不支持 put 方法
【发布时间】:2020-09-23 07:14:56
【问题描述】:

我尝试使用编辑路由更新帖子,但是当我发送表单并使用更新功能时,给我一个错误

我的代码是

<form action="/posts{{$posts->id}}" method="POST">
@method('PUT')
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">

【问题讨论】:

  • 分享你的路线
  • php 工匠路线:列表
  • 您需要将路由定义为Route::put('posts/{id}', ...) 以使其响应PUT 请求
  • 我正在使用相同的更新路线
  • 如果你使用资源控制器路由,你应该使用action="{{ route('posts.update', [ 'post' =&gt; $posts-&gt;id ])) }}" 来确保你得到正确的路由

标签: php laravel


【解决方案1】:

我把我在 laravel 文档上找到的隐藏方法放了,效果很好

<form action="/posts/{{$post->id}}" method="POST">
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}. 
</textarea>
<input type="submit" class="btn btn-primary" value="edit">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

【讨论】:

    【解决方案2】:

    您可以执行以下操作:

    <form action="{{ route('route.name', $post->id) }}" method="POST">
    @csrf
    <label for="">title</label>
    <input type="text" name="title" class="form-control" >
    <label for="">body</label>
    <textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
    <input type="submit" class="btn btn-primary" value="edit">
    

    对于路线:

    Route::post('/posts/{id}', 'Controller@function')->name('route.name');
    

    【讨论】:

      【解决方案3】:

      你必须这样使用

      <form action="{{url('')}}/posts/{{$post->id}}" method="POST">
      @csrf
      <label for="">title</label>
      <input type="text" name="title" class="form-control" >
      <label for="">body</label>
      <textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
      <input type="submit" class="btn btn-primary" value="edit">
      

      在你的路线中这样使用

      Route::post('/posts/{id}', ...)
      

      【讨论】:

      • 为什么在/posts/{{ $post-&gt;id }}之前需要{{url('')}}
      • @TalhaF。这不是强制性的。但写action method href method也是一种方法。
      【解决方案4】:

      您的操作中缺少 / action="/posts/{{ $posts-&gt;id }}"

      【讨论】:

        猜你喜欢
        • 2020-01-29
        • 2020-04-03
        • 2022-06-15
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 2020-02-16
        相关资源
        最近更新 更多