Laravel资源控制器编辑功能不起作用

时间:2019-12-05 12:47:10

标签: php laravel

当我进入编辑页面并想从博客中编辑帖子并进行更改时,它会更改,但在具有该ID的帖子中却不会更改,只会创建具有另一个ID的新帖子

代码:

编辑功能:

     * Show the form for editing the specified resource.
     *
     * @param Integer $id
     * @return Response
     */

    public function edit($id)
    {
        return view('/blog/edit')->with(['blog' => Blog::findOrFail($id)]);
    }

编辑表单:

<form action="/blog" method="post" enctype="multipart/form-data">
                    @csrf
                    <div class="form-group">
                        <label for="formGroupExampleInput"></label>
                        <input type="text" class="form-control" id="formGroupExampleInput" name="titel"
                               placeholder="Title" value="{{ $blog->titel }}">
                    </div>
                    <div class="form-group">
                        <label for="formGroupExampleInput">Lead</label>
                        <textarea class="form-control" name="lead" placeholder="Blog lead">{{ $blog->lead }}</textarea>
                    </div>
                    <div class="form-group">
                        <label for="formGroupExampleInput2">Another label</label>
                        <textarea class="form-control" name="text" placeholder="Blog text">{{ $blog->text }}</textarea>
                    </div>
                    <input type="file" id="real-file" name="bestand" >
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>

谢谢您的时间

1 个答案:

答案 0 :(得分:0)

您必须传递您正在编辑的资源的ID

<form action="/blog/{{ $blog->id }}" method="post" enctype="multipart/form-data"> // <----this line needs id of the resource
    @csrf
    {{method_field('PUT')}} //<----you have to add method PUT
    <div class="form-group">
        <label for="formGroupExampleInput"></label>
        <input type="text" class="form-control" id="formGroupExampleInput" name="titel"
               placeholder="Title" value="{{ $blog->titel }}">
    </div>
    <div class="form-group">
        <label for="formGroupExampleInput">Lead</label>
        <textarea class="form-control" name="lead" placeholder="Blog lead">{{ $blog->lead }}</textarea>
    </div>
    <div class="form-group">
        <label for="formGroupExampleInput2">Another label</label>
        <textarea class="form-control" name="text" placeholder="Blog text">{{ $blog->text }}</textarea>
    </div>
    <input type="file" id="real-file" name="bestand" >
    <button type="submit" class="btn btn-primary">Submit</button>
</form>