当我尝试更新帖子时出现此错误
我想形式上都是正确的,因为我在那里什么都没有改变。
那是我的表单方法:
<form method="post" action="{{ route('games.update', $game->id) }}">
@method('PATCH')
@csrf
</form>
这是我的更新方法:
一切正常,但突然开始出现此错误
public function update(Request $request, $id, Game $game)
{
if ($game->author !== auth()->user()->id || auth()->user()->cannot('edit games'))
abort(404);
$request->validate([
'title' => 'required',
'type' => 'required',
'city' => 'required',
'district' => 'required',
'comment' => 'required',
'full_comment' => 'required',
'photo' => 'sometimes|mimes:jpeg,jpg,gif,bmp,png',
]);
$game = Game::find($id);
$game->title = $request->get('title');
$game->type = $request->get('type');
$game->city = $request->get('city');
$game->district = $request->get('district');
$game->comment = $request->get('comment');
$game->full_comment = $request->get('full_comment');
if ($request->has('photo')) {
$photoName = $game->id . '_photo' . time() . '.' . request()->photo->getClientOriginalExtension();
$game->photo = $request->get('photo');
$game->photo = $photoName;
$request->photo->storeAs('game-photos', $photoName);
}
$game->save();
return redirect('/games')->with('success', 'Game has been updated');
}
答案 0 :(得分:0)
删除第三个形式参数
Game $game
如果您没有在Game模型上实现路线模型绑定
答案 1 :(得分:0)
应该这样工作:
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required',
'type' => 'required',
'city' => 'required',
'district' => 'required',
'comment' => 'required',
'full_comment' => 'required',
'photo' => 'sometimes|mimes:jpeg,jpg,gif,bmp,png',
]);
$game = Game::find($id);
$game->title = $request->get('title');
$game->type = $request->get('type');
$game->city = $request->get('city');
$game->district = $request->get('district');
$game->comment = $request->get('comment');
$game->full_comment = $request->get('full_comment');
if ($game->author !== auth()->user()->id || auth()->user()->cannot('edit games')) {
abort(404);
}
if ($request->has('photo')) {
$photoName = $game->id . '_photo' . time() . '.' . request()->photo->getClientOriginalExtension();
$game->photo = $request->get('photo');
$game->photo = $photoName;
$request->photo->storeAs('game-photos', $photoName);
}
$game->save();
return redirect('/games')->with('success', 'Game has been updated');
}