英文:
Missing required parameter for [Route: one_one.update] [URI: one_one/{one_one}] [Missing parameter: one_one]
问题
我在{{ route('one_one.update',$student->id) }}处遇到了错误,因此无法使用编辑进行CRUD操作,因为出现了这个错误。
路由(我正在使用资源控制器,所以我的路由是这样的)
Route::resource('/one_one', StudentController::class);
edit.blade.php
<form method="POST" action="{{ route('one_one.update',$student->id) }}" enctype="multipart/form-data">
@csrf
@method('PUT')
Name <input type="text" name="name" value="{{ $student->name }}"><br><br>
Contact <input type="text" name="contact" value="{{ $student->contact }}"><br><br>
Email <input type="text" name="email" value="{{ $student->email }}"><br><br>
<h3>Student Details</h3>
Alternative contact <input type="text" name="alter_contact" value="{{ $student->studentdetail->alter_contact }}"><br><br>
Address <input type="text" name="address" value="{{ $student->studentdetail->address }}"><br><br>
Course <input type="text" name="course" value="{{ $student->studentdetail->course }}"><br><br>
<button type="submit">Submit</button>
</form>
index.balde.php
<form action="{{ route('one_one.edit',$student->id) }}"><button type="submit">Edit</button></form>
<form action="{{ route('one_one.destroy',$student->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit">Delete</button></form>
控制器
public function edit(Student $student)
{
return view('one_one.edit',compact('student'));
}
public function update(Request $request, Student $student)
{
$student->update([
'name'=>$request->name,
'contact'=>$request->contact,
'email'=>$request->email,
]);
$student->studentdetail()->update([
'alter_contact'=>$request->alter_contact,
'address'=>$request->address,
'course'=>$request->course,
]);
return redirect()->route('one_one.index')->with('success','Data updated successfully');
}
我也尝试将$student->id作为数组传递,但仍然不起作用。
英文:
I am getting error at {{ route('one_one.update',$student->id) }} and I am unable to use edit for CRUD operation because of this error.
Route (I am using resource controller so my route is this)
Route::resource('/one_one',StudentController::class);
edit.blade.php
<form method="POST" action="{{ route('one_one.update',$student->id) }}" enctype="multipart/form-data">
@csrf
@method('PUT')
Name <input type="text" name="name" value="{{ $student->name }}"><br><br>
Contact <input type="text" name="contact" value="{{ $student->contact }}"><br><br>
Email <input type="text" name="email" value="{{ $student->email }}"><br><br>
<h3>Student Details</h3>
Alternative contact <input type="text" name="alter_contact" value="{{ $student->studentdetail->alter_contact }}"><br><br>
Address <input type="text" name="address" value="{{ $student->studentdetail->address }}"><br><br>
Course <input type="text" name="course" value="{{ $student->studentdetail->course }}"><br><br>
<button type="submit">Submit</button>
</form>
index.balde.php
<form action="{{ route('one_one.edit',$student->id) }}"><button type="submit">Edit</button></form>
<form action="{{ route('one_one.destroy',$student->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit">Delete</button></form>
Controller
public function edit(Student $student)
{
return view('one_one.edit',compact('student'));
}
public function update(Request $request, Student $student)
{
$student=Student::update([
'name'=>$request->name,
'contact'=>$request->contact,
'email'=>$request->email,
]);
$student->studentdetail()->update([
'alter_contact'=>$request->alter_contact,
'address'=>$request->address,
'course'=>$request->course,
]);
return redirect()->route('one_one.index')->with('success','Data updated successfully');
}
I also tried to pass $student->id as array but it is also not working.
答案1
得分: 0
你正在将一个空的不存在的模型传递给你的视图。对于你的 edit
方法,没有发生隐式路由模型绑定。路由参数的名称与控制器的 edit
方法的类型提示参数不匹配。你得到的是方法注入(一个新实例的 Student
模型),而不是隐式路由模型绑定。
你资源路由的路由参数是 one_one
(因为这是资源的名称),而不是 student
。你的方法签名必须调整以进行隐式路由模型绑定:
public function edit(Student $one_one)
Laravel 10.x 文档 - 路由 - 路由模型绑定 - 隐式绑定
英文:
You are passing an empty non-existing model to your view. There is no Implicit Route Model Binding happening for your edit
method. The route parameter name is not matching the typehinted parameter of the Controller's edit
method. You are getting method injection (a new instance of a Student
model) instead of Implicit Route Model Binding
Your route parameter for your resource routes is one_one
(as that is the name of the resource) not student
. Your method signature would have to be adjusted for Implicit Route Model Binding:
public function edit(Student $one_one)
Laravel 10.x Docs - Routing - Route Model Binding - Implicit Binding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论