Laravel 8使用StaffController编辑Post时路由缺少必填参数如何解决
问题复现
我使用新建的StaffController编辑Post时,运行出现路由缺少必填参数的错误,错误信息如下:
Missing required parameter for [Route: verification.update] [URI: verification/{verification}] [Missing parameter: verification].
控制器代码
public function edit(Request $request, Post $post) { $choices = Choice::all(); $pets = Pet::all(); $types = Type::all(); $vehicles = Vehicle::all(); $genders = Gender::all(); $reasons = Reason::all(); $posts = Post::all(); return view('dashboard.staff.edit',compact('post', 'types', 'vehicles', 'pets', 'choices', 'genders', 'reasons')); } public function update(Request $request, Post $post) { $request->validate([ 'phonenumber' => 'required', ]); $input = $request->all(); if ($image = $request->file('image')) { $destinationPath = 'image/'; $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension(); $image->move($destinationPath, $profileImage); $input['image'] = "$profileImage"; }else{ unset($input['image']); } $post->update($input); return back()->with('success', 'Post verification successfully.'); } public function destroy(Post $post, $id) { $this->authorize('isStaff'); $post = Post::find($id); $post->delete(); return back()->with('success','Post deleted successfully'); }
路由定义
Route::resource('verification', StaffController::class);
视图表单代码
<form action="{{ route('verification.update', ['verification'=>$post->id]) }}" x-data="{types: {{$post->type_id}}}" method="POST" enctype="multipart/form-data"> @csrf @method('PUT')
修复方案
这个错误的核心原因是Laravel资源路由的默认参数名和控制器隐式模型绑定的参数名不匹配:你定义的verification资源路由生成的路由参数名为verification,但控制器里对应方法的注入参数名为post,导致框架无法正常解析参数。可以选择以下任意一种方案修复:
方案1:修改控制器参数名匹配路由参数
将控制器中edit、update、destroy方法的注入参数名改为和路由参数一致的$verification,同时修正destroy方法重复接收参数的问题:
// 编辑方法 public function edit(Request $request, Post $verification) { $choices = Choice::all(); $pets = Pet::all(); $types = Type::all(); $vehicles = Vehicle::all(); $genders = Gender::all(); $reasons = Reason::all(); $posts = Post::all(); // 保持视图变量不变,将verification赋值给post传给模板 return view('dashboard.staff.edit',compact('types', 'vehicles', 'pets', 'choices', 'genders', 'reasons'))->with('post', $verification); } // 更新方法 public function update(Request $request, Post $verification) { $request->validate([ 'phonenumber' => 'required', ]); $input = $request->all(); if ($image = $request->file('image')) { $destinationPath = 'image/'; $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension(); $image->move($destinationPath, $profileImage); $input['image'] = $profileImage; }else{ unset($input['image']); } $verification->update($input); return back()->with('success', 'Post审核成功'); } // 删除方法,移除多余的$id参数 public function destroy(Post $verification) { $this->authorize('isStaff'); $verification->delete(); return back()->with('success','Post删除成功'); }
方案2:修改资源路由参数名匹配控制器
如果不想改动控制器逻辑,可以在定义资源路由时手动指定参数名,将路由参数改为post:
Route::resource('verification', StaffController::class)->parameters([ 'verification' => 'post' ]);
内容的提问来源于stack exchange,提问作者raihan
相关产品推荐
相关产品推荐

