簡體   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