繁体   English   中英

缺少 [Route: 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 ]) }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM