无法在 Laravel 8 中更新数据

时间:2021-07-27 04:47:00

标签: php mysql laravel-8

目前,我正在写博客。但是当我尝试更新帖子时,它没有更新,也没有重现错误。

代码有什么问题?任何人,请给我一些建议来即兴创作我的代码。我想了解是什么问题。所以,任何人都可以详细解释一下,我会很感激这件事。

PostController 代码:-

 public function update(Store $request,Post $post)
  {
        
    $data = $request->validated();
    $post = $post->update($data);
    return redirect('/index')->withMessage('successfully published');
  }

查看页面代码:-

<form method="POST" action='/update'>
      @csrf
      <table class="table table table-striped table">
    <div class="form-group">
        <label for="name" class="col-form-label text-md-right">Title</label><br>
    <input required="required" placeholder="Enter title here" class="" type="text" name="title" class="" value="{{$data->title}}" required/>
  </div>
  <div class="form-group">
    <label for="name" class="col-form-label text-md-right">Description</label>
  </div>
    <div>
    <textarea name="description" class="" required>{{$data->description}}
    </textarea></div>
  <div>  
    <input type="submit" name="update" class="btn btn-success" value="Update" required />
  </div>
</table>
</form>

路由文件代码:-

Route::get('posts/edit/{edit}',[PostController::class,'edit']);
    Route::post('/update',[PostController::class,'update']);

1 个答案:

答案 0 :(得分:0)

将您的路线 update 更改为:

Route::post('posts/update',['as'=>'update','uses'=>'PostController@update']);

在查看页面中

<form method="POST" action="{{ route('update') }}">