【问题标题】:PUT method not supported for this route. Supported methods: GET, HEAD, POST Laravel 6此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST Laravel 6
【发布时间】:2020-01-23 15:15:32
【问题描述】:

我正在使用 Laravel 6 制作编辑表单。显然,这是一个普遍的问题,我在这里查找了如何解决它,我尝试以 5 种方式放置隐藏的 csrf 字段,并且每次运行时都出现相同的错误,所以 IDK 如果这些解决方案已被 Laravel 6 或我弃用我做错了什么。

edit.blade.php

<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="form-group">
        <label for="exampleFormControlInput1">Email address</label>
        <input type="email" name="email" value="{{ $post->email }}" class="form-control"
               id="exampleFormControlInput1">
    </div>
    <div class="form-group">
        <label for="exampleFormControlInput1">Name</label>
        <input type="text" name="name" value="{{ $post->name }}" class="form-control"
               id="exampleFormControlInput2" placeholder="Name">
    </div>
    <label for="exampleFormControlInput1">Image</label>
    <div class="form-group row">
        <div class="col-sm-2">
            @if($post->image)
                <img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
            @endif
        </div>
        <input type="file" name="image" value="{{ $post->image }}"
               id="exampleFormControlInput3">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

PostsController.php

    public function edit(Post $post)
    {
        return view ('posts.edit', compact('post'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        $post->update($request->all());
        $post->name = $request->name;
        $post->email = $request->email;

        if(Input::hasFile('image')){
        $file = Input::file('image');
        $path = time().$file->getClientOriginalName();
        $destinationPath = public_path(). '/images/';
        $filename = time().$file->getClientOriginalName();
        $file->move($destinationPath, $filename);

       //then proceeded to save
       $post->image = $destinationPath.$filename;
       $post->save();
       }

       else
        $post->save();

    return redirect('posts.all');
    }

我的路线,以防万一

Route::resource('posts', 'PostsController');

这些是我尝试编写 csrf 字段的其他方式。

方式一:

{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">

方式二:

@csrf_field
{{ method_field('PUT') }}

方式3:

@csrf
{{ method_field('PATCH') }}

方式4:

@csrf
@method('PUT')

所有这些都导致我看到相同的错误消息。

【问题讨论】:

标签: php laravel laravel-6


【解决方案1】:

尝试替换这个:

<form method="POST" action="/posts/{{$post->edit}}" 

用这个:

<form method="POST" action="{{ route('posts.update', [$post->id]) }}" 

我认为您正在尝试在不带任何参数的 /posts/ 路由上发布,因为 $post->edit 可能不会返回帖子的 id :)

【讨论】:

  • 正在写这个确切的答案 xD,你打败了我
  • 是的,我知道为什么它会显示该消息而不是像我没有定义编辑变量 lmao 之类的东西
猜你喜欢
  • 2020-04-25
  • 1970-01-01
  • 2019-12-29
  • 2020-05-27
  • 2020-04-03
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
相关资源
最近更新 更多