[路线:questions.update] 缺少必需的参数,在 url 中传递 2 个参数
Missing required parameters for [Route: questions.update] pass 2 parameters in the url
伙计们,我在文件 web.php
中的 url 中传递两个参数时遇到问题
那是我的错误
我用过
Route::resource('/exams/{exam}/questions', 'Backend\QuestionController');
定义默认路由。
在终端中看起来像:
| | PUT|PATCH | exams/{exam}/questions/{question} | questions.update | App\Http\Controllers\Backend\QuestionController@update | web |
| | | | | | auth |
| | DELETE | exams/{exam}/questions/{question} | questions.destroy | App\Http\Controllers\Backend\QuestionController@destroy | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/edit | questions.edit | App\Http\Controllers\Backend\QuestionController@edit | web |
| | | | | | auth |
而要重定向,是这样写的
<a href="{{ route('questions.edit', [ $exams->id, $question->id ]) }}"class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a>
和Controller.php
public function edit(Exam $exams, Question $questions, Category $category){
return view('question.edit', compact( 'exams' ,'questions', 'category'));
}
public function update(QuestionUpdateRequest $request, $id){
$questions = Question::find($id);
$questions->description = $request->get('description');
$questions->iframe = $request->get('iframe');
$questions->image = $request->get('image');
$questions->exam_id = $request->get('exam_id');
$questions->category_id = $request->get('category_id');
$questions->save();
return redirect()->route('question.index', $questions->exam_id);
}
但是我不明白为什么它会失败,如果我已经传递了 url 指示的参数。
同时添加编辑视图
<form action="{{ route('questions.update', $questions->id ) }}" method="POST" enctype="multipart/form-data">
{{--/exams/{{$exams->id}}/questions--}}
<div class="form-group">
{{--<input type="hidden" name="question_id" value="{{$questions->id}}">--}}
<label for="description">Descripcion de la pregunta*</label>
<textarea name="description" type="text"
class="form-control" id="description"
aria-describedby="descriptionHelp"
placeholder="Inserte la pregunta">{{ old('description', $questions->description) }}</textarea>
<small id="descriptionHelp"
class="form-text text-muted">Escribe la descripcion de la pregunta.</small>
</div>
<div class="form-group">
<label for="iframe">Video asociado *</label>
<textarea name="iframe" type="text"
class="form-control" id="iframe"
aria-describedby="iframeHelp"
placeholder="Inserte la URL del video">{{ old('iframe', $questions->iframe) }}</textarea>
<small id="iframeHelp" class="form-text text-muted">Inserta la url del video.</small>
</div>
<div class="form-group d-flex flex-column">
<label for="image">Imagen asociada</label>
<input name="image" type="file" class="py-1">
</div>
<div class="form-group">
<label for="category">A que categoria pertenece</label>
<select name="category_id" class="form-control form-control-lg" id="category_id">
@foreach($category as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<small id="selectHelp" class="form-text text-muted">Elige una categoria.</small>
</div>
<hr />
@CSRF
@method('PUT')
<button type="submit" class="btn btn-primary">Guardar pregunta</button>
</form>
| | PUT|PATCH | exams/{exam}/questions/{question} | questions.update | App\Http\Controllers\Backend\QuestionController@update | web |
那是你的路线,但是,
<form action="{{ route('questions.update', $questions->id ) }}" method="POST" enctype="multipart/form-data">
这是你的行动。
您在表单操作中缺少考试 ID。
应该是
{{ route('questions.update', [ $exams->id, $question->id ]) }}
伙计们,我在文件 web.php
那是我的错误
我用过
Route::resource('/exams/{exam}/questions', 'Backend\QuestionController');
定义默认路由。
在终端中看起来像:
| | PUT|PATCH | exams/{exam}/questions/{question} | questions.update | App\Http\Controllers\Backend\QuestionController@update | web |
| | | | | | auth |
| | DELETE | exams/{exam}/questions/{question} | questions.destroy | App\Http\Controllers\Backend\QuestionController@destroy | web |
| | | | | | auth |
| | GET|HEAD | exams/{exam}/questions/{question}/edit | questions.edit | App\Http\Controllers\Backend\QuestionController@edit | web |
| | | | | | auth |
而要重定向,是这样写的
<a href="{{ route('questions.edit', [ $exams->id, $question->id ]) }}"class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a>
和Controller.php
public function edit(Exam $exams, Question $questions, Category $category){
return view('question.edit', compact( 'exams' ,'questions', 'category'));
}
public function update(QuestionUpdateRequest $request, $id){
$questions = Question::find($id);
$questions->description = $request->get('description');
$questions->iframe = $request->get('iframe');
$questions->image = $request->get('image');
$questions->exam_id = $request->get('exam_id');
$questions->category_id = $request->get('category_id');
$questions->save();
return redirect()->route('question.index', $questions->exam_id);
}
但是我不明白为什么它会失败,如果我已经传递了 url 指示的参数。
同时添加编辑视图
<form action="{{ route('questions.update', $questions->id ) }}" method="POST" enctype="multipart/form-data">
{{--/exams/{{$exams->id}}/questions--}}
<div class="form-group">
{{--<input type="hidden" name="question_id" value="{{$questions->id}}">--}}
<label for="description">Descripcion de la pregunta*</label>
<textarea name="description" type="text"
class="form-control" id="description"
aria-describedby="descriptionHelp"
placeholder="Inserte la pregunta">{{ old('description', $questions->description) }}</textarea>
<small id="descriptionHelp"
class="form-text text-muted">Escribe la descripcion de la pregunta.</small>
</div>
<div class="form-group">
<label for="iframe">Video asociado *</label>
<textarea name="iframe" type="text"
class="form-control" id="iframe"
aria-describedby="iframeHelp"
placeholder="Inserte la URL del video">{{ old('iframe', $questions->iframe) }}</textarea>
<small id="iframeHelp" class="form-text text-muted">Inserta la url del video.</small>
</div>
<div class="form-group d-flex flex-column">
<label for="image">Imagen asociada</label>
<input name="image" type="file" class="py-1">
</div>
<div class="form-group">
<label for="category">A que categoria pertenece</label>
<select name="category_id" class="form-control form-control-lg" id="category_id">
@foreach($category as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<small id="selectHelp" class="form-text text-muted">Elige una categoria.</small>
</div>
<hr />
@CSRF
@method('PUT')
<button type="submit" class="btn btn-primary">Guardar pregunta</button>
</form>
| | PUT|PATCH | exams/{exam}/questions/{question} | questions.update | App\Http\Controllers\Backend\QuestionController@update | web |
那是你的路线,但是,
<form action="{{ route('questions.update', $questions->id ) }}" method="POST" enctype="multipart/form-data">
这是你的行动。 您在表单操作中缺少考试 ID。
应该是
{{ route('questions.update', [ $exams->id, $question->id ]) }}