laravel - Laravel 在 url 中传递多个参数并取特定参数
问题描述
在我需要为每个 url 传递 2 个参数来插入它们之前,因为它们是以交错的方式进行的,也就是说。首先我创建一个考试,然后该考试有问题。为此,我决定沿路线传递 id
Route::resource('/exams/{exam}/questions');
为此,我选择在我需要的方法中像这样传递它:
Route::get('/exams/{exam}/questions/{id}/edit', function ($examId, $questionId){})
->name('questions.edit');
现在,问题有答案并引用它,我创建了这样的资源路径:
Route::resource('/exams/{exam}/questions/{question}/answers', 'Backend\AnswerController');
问题是,当我尝试保存答案时,它会产生 2 个问题......
- 第一个,当我保存答案时,它会抛出这个错误。
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: answers.index] [URI: exams/{exam}/questions/{question}/answers].
- 其次,无论您将其保存在哪个问题中(如果它的 id 为 2、3、4 或 N),答案始终将 question_id = 1 保存为已保存;
添加我的控制器和路由
| | GET|HEAD | exams/{exam}/questions/{question}/answers | answers.index | App\Http\Controllers\Backend\AnswerController@index | web |
| | | | | | auth |
| | POST | exams/{exam}/questions/{question}/answers | answers.store | App\Http\Controllers\Backend\AnswerController@store | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/answers/create | answers.create | App\Http\Controllers\Backend\AnswerController@create | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/answers/{answer} | answers.show | App\Http\Controllers\Backend\AnswerController@show | web |
| | | | | | auth |
| | PUT|PATCH | exams/{exam}/questions/{question}/answers/{answer} | answers.update | App\Http\Controllers\Backend\AnswerController@update | web |
| | | | | | auth |
| | DELETE | exams/{exam}/questions/{question}/answers/{answer} | answers.destroy | App\Http\Controllers\Backend\AnswerController@destroy | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/answers/{answer}/edit | answers.edit | App\Http\Controllers\Backend\AnswerController@edit | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/answers/{id}/confirmDelete | answers.confirmDelete | App\Http\Controllers\Backend\AnswerController@confirmDelete | web |
我的控制器
public function index($examId, $questionId){
$exams = Exam::findOrFail($examId);
$questions = Question::findOrFail($questionId);
$answers = Answer::all();
return view('answer.index', compact('exams', 'questions','answers'));
}
public function create($examId, $questionId){
$exams = Exam::find($examId);
$questions = Question::find($questionId);
/*.
$isCorrect = Answer::where('is_correct', 1)->get();
$isWrong = Answer::where('is_correct', 0)->get();
*/
return view('answer.create', compact('exams', 'questions'));
}
public function store(AnswerStoreRequest $request, $questionId, Exam $exams){
$answers = new Answer();
$answers->description = $request->get('description');
$answers->iframe = $request->get('iframe');
$answers->image = $request->get('image');
$answers->is_correct = $request->get('is_correct');
$answers->question_id = $questionId;
$answers->save();
return redirect()->route('answers.index', $exams->id ,$answers->question_id);
}
我遇到的问题有什么想法或解决方案吗?
编辑 1
添加创建答案视图。
<form action="{{ route('answers.store', [$exams->id, $questions->id]) }}" method="POST">
改变web.php
Route::get('/exams/{exam}/questions/{question}/answers', ['as' => 'answers.index', 'uses' => 'Backend\AnswerController@index']);
Route::get('/exams/{exam}/questions/{question}/answers/create', ['as' => 'answers.create', 'uses' => 'Backend\AnswerController@create']);
Route::post('/exams/{exam}/questions/{question}/answers/', ['as' => 'answers.store', 'uses' => 'Backend\AnswerController@store']);
Route::get('exams/{exam}/questions/{question}/answers/{answer}/edit', ['as' => 'answers.edit', 'uses' => 'Backend\AnswerController@edit']);
Route::put('exams/{exam}/questions/{question}/answers/{answer}', ['as' => 'answers.update', 'uses' => 'Backend\AnswerController@update']);
Route::delete('exams/{exam}/questions/{question}/answers/{answer}', ['as' => 'answers.destroy' , 'uses' => 'Backend\AnswerController@destroy']);
Route::get('/exams/{exam}/questions/{question}/answers/{id}/confirmDelete',['as' => 'answers.confirmDelete', 'uses' => 'Backend\AnswerController@confirmDelete']);
我还添加了我显示问题的索引的 foreach。
@foreach($questions->answers as $answer)
<tr>
<td>{{ $answer->id }}</td>
<td>{{ $answer->description }}</td>
<td>
<a href="{{ route('answers.edit', [$exams->id, $questions->id, $answer->id]) }}"
class="btn btn-warning btn-sm"><i class="far fa-edit"></i>
</a>
<a href="{{ route('answers.confirmDelete', [$exams->id, $questions->id, $answer->id]) }}"
class="btn btn-danger btn-sm"><i class="fas fa-trash-alt"></i>
</a>
</td>
</tr>
@endforeach
解决方案
当您处理多个参数时,您传递的多个参数是错误的,您需要指定哪个值属于哪个参数。
所以改变
return redirect()->route('answers.index', $exams->id ,$answers->question_id);
到
return redirect()->route('answers.index', ['examId' => $exams->id ,'questionId' => $answers->question_id]);
请参阅laravel 文档以获得准确的解释
编辑:您似乎也没有传递第三个必需参数。我建议您像这样手动定义 GET、POST、PUT、PATCH、DELETE 方法。
// Example
Route::get('example', ['as' => 'example.index', 'uses' => 'MyController@index']); // show all items
Route::get('example/create', ['as' => 'example.create', 'uses' => 'MyController@create']); // show the add new items
Route::post('example', ['as' => 'example.store', 'uses' => 'MyController@store']); // store new items
Route::get('example/edit/{id}', ['as' => 'example.edit', 'uses' => 'MyController@edit']); // edit existing items
Route::patch('example/update/{id}', ['as' => 'example.update', 'uses' => 'MyController@update']); // update existing items
Route::delete('example/delete/{id}', ['as' => 'example.delete', 'uses' => 'MyController@destroy']); // delete existing items
推荐阅读
- workflow - 将工作流从 SharePoint Designer 2013 发布到 SharePoint Online 时出错
- docker - Docker 舞会/Prometheus 容器退出
- go - HTTP 客户端返回结果为 null
- azure-active-directory - 通过 microsoft graph 的来宾用户列表
- check-mk - 通过 CheckMK Web-API 获取主机状态
- sed - 正则表达式捕获后附加 sed
- linux - Git 每天自动提交
- iframe - 从 iframe 页面在父页面中设置隐藏字段值
- cobol - 比较字段时 SOC7 异常
- c - 从C中的文件中读取矩阵