【问题标题】:Missing required parameter for Route Laravel 8Route Laravel 8 缺少必需的参数
【发布时间】:2021-11-06 16:48:09
【问题描述】:

我想用新的控制器名称 StaffController 编辑帖子,但出现错误 Missing required parameter for Route

缺少 [Route: verify.update] [URI: verify/{verification}] [缺少参数: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')

路线列表 enter image description here

【问题讨论】:

  • 您在控制器中请求Post 对象的操作中发送id。也许在更新函数中删除$post的类型

标签: laravel


【解决方案1】:

因为你的路线是:

Route::resource('verification', StaffController::class);

您需要您的控制器方法使用正确的参数名称:

public function edit(Post $verification)
{

}

public function update(Request $request, Post $verification)
{

}

这是因为 Laravel 正在尝试使用参数名称解​​析您的绑定。

发生了什么:

  • Laravel 在你的编辑方法中找不到Post $post(由于上面的原因,它应该是Post $verification,或者你应该重命名你的路由并将verifications替换为posts
  • 它会创建一个空的 Post 实例
  • 它将这个空实例发送到视图
  • 您正在使用此空实例在表单操作属性中构建路由
  • 由于为空,$post-&gt;id 返回 null,因此您的 url 中没有参数名称 verification... 这导致
Missing required parameter for [Route: verification.update] 
[URI: verification/{verification}] [Missing parameter: verification].

【讨论】:

    【解决方案2】:
    Route::resource('verification', StaffController::class)->name('verification');
    

    你可以试试这个。

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 2023-03-19
      • 2021-06-09
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2021-11-03
      • 2021-07-20
      • 2018-10-28
      相关资源
      最近更新 更多