首页 > 解决方案 > 如何为更新帖子制作路由?

问题描述

我已经定义了更新帖子的路线,但发生了错误。

这是我的 web.php:

Route::get('post','PostController@index')->name('post.index');
Route::post('post','PostController@store')->name('post.store');
Route::get('post/create','PostController@create')->name('post.create');
Route::put('post/{id}','PostController@update')->name('post.update');
Route::delete('post/{id}','PostController@destroy')->name('post.destroy');
Route::get('post/{id}/edit','PostController@edit')->name('post.edit');

这是我的 Postcontroller.php:

public function edit($id)
    {
        $post= Post::FindorFail($id);
        //dd($records);
        return view('post.edit',compact('post'));
    }

    public function update(Request $request, $id)
    {
        $post= Post::findOrFail($id);

       $post->title->request->title;
       $post->save();

        return redirect('post');
    }

这是我的帖子/edit.php:

 {!! Form::model($post,['method'=>'PATCH','action'=>['PostController@update', $post->id]]) !!}

    <div class="form-group">
        {{Form::label('title','Title')}}
        {{Form::text('title',$post->title,['id'=>'article-creditor','class'=>'form-control'])}}
    </div>
    {!! Form::submit('Submit',['class'=>'btn btn-primary']) !!}
    {!! Form::close() !!}

标签: laravel

解决方案


你能试试这个吗

 {!! Form::model($post,['method'=>'POST','action'=>['PostController@update', $post->id]]) !!}

 @method('PUT')

<div class="form-group">
    {{Form::label('title','Title')}}
    {{Form::text('title',$post->title,['id'=>'article-creditor','class'=>'form-control'])}}
</div>
{!! Form::submit('Submit',['class'=>'btn btn-primary']) !!}
{!! Form::close() !!}

基本上我只是将表单中的方法编辑为 POST 然后添加@method('PUT')我认为表单本身没有 put 方法,因此应该将其分开。


推荐阅读