嘿,我想知道是否有人可以帮助,我需要编辑和更新一个帖子,我已经得到了编辑部分的工作,但更新我已经卡住了几个小时
编辑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']);
为postController
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');
}
为PostController
//edit returns a view
public function edit(Post $post)
{
return view('edit', ['post' => $post]);
}
更新/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param AppModelsPost $post
* @return IlluminateHttpResponse
*/
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');
}
在所有注释之后…
<form action="/posts" method="Post">
应该
<form action="/posts/{{ $post->id }}" method="Post">
<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>
将整个对象传递给route,而不是传递id