【发布时间】:2021-05-02 11:07:35
【问题描述】:
嘿,我想知道是否有人可以提供帮助,我需要编辑和更新帖子,我已经完成了编辑部分,但更新我已经坚持了好几个小时了
编辑blade.php
<h2 class="text-center">Edit</h2>
<form action="{{ route('posts.update',['post' => $post]) }}" method="Post">
@method('PUT')
@csrf
<textarea input="" class="col-12 pb-5 mt-4" name="edit">{{$post->content}}
</textarea>
<div class="row justify-content-center">
<button type="submit" class="btn btn-primary col-2 mt-4">Edit</button>
</div>
</form>
@endsection
web.php
Route::get('/posts/{post}/edit', [PostController::class, 'edit'])->middleware(['auth']);
Route::put('post/{post}','PostController@update')->middleware(['auth']);
后控制器
public function store(Request $request)
{
//get the current user
$user = Auth::user();
//attach the content to allow comment
$user->posts()->create([
'content' => $request->input('content'),
'allowComment' => true,
]);
// return to posts page
return redirect('/posts');
}
后控制器
//edit returns a view
public function edit(Post $post)
{
return view('edit', ['post' => $post]);
}
更新
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
//get the current user
$user = Auth::user();
//attach the content to allow comment
$user->posts()->create([
'content' => $request->input('content'),
'allowComment' => true,
]);
// return to posts page
return redirect('/posts');
}
【问题讨论】:
-
欢迎来到 SO ... 那有什么问题?
-
嘿,我有一个帖子,我可以编辑帖子,但我无法更新帖子,我是初学者,我认为它只是找不到更新的帖子
-
为什么不能,问题是什么,实际发生了什么,是不是报错了......
-
所以我收到此错误 rn 此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。
-
但我认为它根本没有链接到更新的功能