【问题标题】:Can't update data in Laravel 8无法在 Laravel 8 中更新数据
【发布时间】:2021-10-02 22:31:24
【问题描述】:

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

代码有什么问题?任何人,请给我一些建议来即兴我的代码。我想了解问题所在。所以,任何人都可以详细解释,那么我将非常感激这件事。

后控制器代码:-

 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']);

【问题讨论】:

    标签: php mysql laravel-8


    【解决方案1】:

    将您的路线 update 更改为:

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

    在查看页面中

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

    【讨论】:

    • 感谢您的建议,但现在它给了我->''ReflectionFunction::__construct() 期望参数1为字符串,给定数组”,点击更新按钮后出现此类错误跨度>
    • 实际上,现在我在我的代码中进行了一些更改。但现在它创建了一条新记录而不是编辑数据。
    • 现在可以工作了吗?还是同样的问题仍然存在?
    • 是的,现在它可以正常工作了,非常感谢你帮助我
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2016-03-07
    • 2021-09-25
    • 2020-02-19
    • 1970-01-01
    • 2016-12-12
    • 2020-07-20
    • 1970-01-01
    相关资源
    最近更新 更多