Edit record in laravel 5.3

3

I am developing an application that performs a series of questions that are organized into categories, there are 2 questions by category, after the second question of the category is done, it averages the score between the two and saves it in the Bd, I do this way.

public function question_two(Request $request)
{
  $promedio = ($request->record + $request->record02)/2;

  $user_id = Auth::user()->user_id;
  $group_id = Auth::user()->group_id;
  $student_id = $request->student_id;
  $test = Test::where('student_id', $student_id)->get()->first();
  $test_id = $test->id;

  $report = new Report();

  $report->test_id = $test_id;
  $report->user_id = $user_id;
  $report->student_id = $student_id;
  $report->enum_pts = $promedio;
  $report->calculos_m = 0;
  $report->seriacion = 0;
  $report->p_enunciados = 0;
  $report->t_operacionales = 0;
  $report->p_escala = 0;
  $report->comparacion_n = 0;

  $report->save();

  return view('test/question3', compact('student_id'));

}

The function saves correctly, the problem is that when I continue the test I must edit the table because when I create it, I only enter the results of the first item and the others fill them with zero, but it does not work in this way :

public function question_four(Request $request){

  $calculos_m = ($request->score + $request->record)/2;

  $report = Report::where('student_id', $request->student_id)->get();
  $report->calculos_m = $calculos_m;
}

I do dd of the report and then it receives the property but it does not keep it in the Bd.

Collection {#262 ▼
#items: array:1 [▶]
+"calculos_m": 3.5
}
    
asked by DVertel 06.11.2016 в 15:35
source

1 answer

2

You are using get() that gets an array of results, you should use first() to get a single record and use the save() method to save the desired record.

Small note: it is always preferable to use dependency injection instead of instantiating a class directly in the controller.

    
answered by 06.11.2016 / 16:33
source