Skip to main content
3 de 3
agregado datos extras
Edgar Gc
  • 957
  • 7
  • 20

Actualizar los datos de una colleccion en laravel

desde hace semanas tengo un problema con la vista de actualizar (y los metodos edit/update). Y es lo siguiente:

Si tengo un Examen este tiene sus propias Preguntas

Examen1
     -> Pregunta 1
     -> Pregunta 2
     -> Pregunta 3

Examen2
     -> Pregunta 4
     -> Pregunta 5
     -> Pregunta 6

Examen N
     -> Pregunta N
     -> Pregunta N+1
     -> Pregunta N+1

El detalle radica en que, cuando entro a cualquier Examen (en este caso Examen 2) e intento editar cualquier pregunta, por ejemplo Pregunta 4. Lo que obtengo es la vista edit.blade.php con los datos de la primera pregunta del primer examen, es decir, me filtra así:

Examen1
     -> Pregunta 1

Adjunto mi controller

public function edit($id){
        $exams = Exam::find($id);
        $questions = Question::find($id);
        $category = Category::all();

        return view('question.edit', compact('exams', 'questions', 'category'));
    }

    public function update(QuestionUpdateRequest $request, $id){
        $exams = Exam::find($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);
    }

el index.blade.php

<table class="table">
    <thead class="thead">
        <tr>
            <th>ID</th>
            <th>Descripcion</th>
            <th>Categoria</th>
            <th>Acciones</th>
        </tr>
    </thead>
    <tbody>
    @foreach($exams->questions as $question)
        <tr>
            <td>{{$question->id}}</td>
            <td>{{$question->description}}</a></td>
            <td>{{$question->category->name}}</td>
            <td>
                <a href="#" class="btn btn-success btn-sm"><i class="fas fa-plus-square"></i></a>
                <a href="{{ route('questions.edit', [ $exams->id, $question->id ]) }}"
                   class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a>
                <a href="#" class="btn btn-danger btn-sm"><i class="fas fa-trash-alt"></i></a>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>

Cuando ejecuto el dd() me arroja esto:

App\Question {#1288 ▼
  #fillable: array:5 [▶]
  #guarded: []
  #connection: "mysql"
  #table: "questions"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▼
    "id" => 9
    "exam_id" => 1
    "category_id" => 2
    "description" => "iwqueo"
    "iframe" => "wpqueoiq"
    "image" => null
    "order" => null
    "created_at" => "2020-08-19 04:26:13"
    "updated_at" => "2020-08-19 04:26:13"
  ]
  #original: array:9 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
}

Y mi pregunta es ¿Cómo puedo filtrar en el edit para que me muestre la pregunta que intento editar?

EDIT 1

Agrego el como tengo las rutas de mi proyecto.

En mi archivo web, uso

Route::resource('/exams/{exam}/questions', 'Backend\QuestionController');

Esto me genera las siguientes rutas en automatico...

|        | GET|HEAD  | exams/create                           | exams.create       | App\Http\Controllers\Backend\ExamController@create                     | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | GET|HEAD  | exams/{exam}                           | exams.show         | App\Http\Controllers\Backend\ExamController@show                       | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | PUT|PATCH | exams/{exam}                           | exams.update       | App\Http\Controllers\Backend\ExamController@update                     | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | DELETE    | exams/{exam}                           | exams.destroy      | App\Http\Controllers\Backend\ExamController@destroy                    | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | GET|HEAD  | exams/{exam}/edit                      | exams.edit         | App\Http\Controllers\Backend\ExamController@edit                       | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | POST      | exams/{exam}/questions                 | questions.store    | App\Http\Controllers\Backend\QuestionController@store                  | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | GET|HEAD  | exams/{exam}/questions                 | questions.index    | App\Http\Controllers\Backend\QuestionController@index                  | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | GET|HEAD  | exams/{exam}/questions/create          | questions.create   | App\Http\Controllers\Backend\QuestionController@create                 | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | GET|HEAD  | exams/{exam}/questions/{question}      | questions.show     | App\Http\Controllers\Backend\QuestionController@show                   | web        |
|        |           |                                        |                    |                                                                        | auth       |
|        | 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       |
Edgar Gc
  • 957
  • 7
  • 20