此路线不支持PUT方法。支持的方法:GET,HEAD,POST Laravel 6

时间:2019-09-23 14:08:45

标签: php laravel laravel-6

我正在使用Laravel 6进行编辑。显然,这是一个普遍的问题,我在这里查找了如何解决它,我尝试以5种方式放置隐藏的csrf字段,并且每次都以相同的错误运行,因此如果Laravel 6或I不赞成使用这些解决方案,则应使用IDK做错了事。

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字段的其他方式。

方法1:

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

方法2:

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

方法3:

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

方法4:

@csrf
@method('PUT')

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

1 个答案:

答案 0 :(得分:1)

尝试替换为:

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

与此:

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

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