laravel - 如何为更新帖子制作路由?
问题描述
我已经定义了更新帖子的路线,但发生了错误。
这是我的 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() !!}
解决方案
你能试试这个吗
{!! 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 方法,因此应该将其分开。
推荐阅读
- rust - 我可以强制一个特征是协变的吗?
- sql - 使用从 CASE 语句返回的相同值填充整个列
- android - google api java客户端在使用proguard-android-optimize.txt时未传递值为0的参数
- c# - 是否有任何工具可以为 wpf (xaml) 中的复杂控件创建实时样式
- html - 未正确对齐下拉内容
- android - 在 Android Oreo 及更高版本上通过 FCM 通知唤醒我的应用
- c# - 在 Winforms 中使用 printdocument 打印时更新控件
- reactjs - 如何使用 React 制作垂直标签
- google-oauth - 如何在没有来自渐进式 Web 应用程序的持续明确用户同意的情况下使用 Google API?
- openbugs - 使用 inprod() 总结线性预测器的问题