In Laravel 5.4 I'm trying to save the contents of an N: M relationship in a pivot, the problem I have is that it updates all the records that are related to that id, it's like I did an update with the where of the id of the student.
The code:
$alumnos = Alumnos::obtenerAlumnosAsignaturas();
foreach($alumnos as $alumno){
if($alumno->pivot->asignatura == null){
$alumno->pivot->asignatura = 0;
$alumno->pivot->save();
}
}
The two tables
| id | alumno| |id|asignaturas |
|----|-------| |--|------------|
| 1 | Juan | | 1| Matemáticas|
| 2 | Pepe | | 2| Lengua |
| 3 | Luis | | 3| Inglés |
|----|-------| |--|------------|
Pivot
| id | asignatura_id | alumno_id |
|----|---------------|-----------|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 3 | 3 | 3 |
After executing the code, it goes to:
| id | asignatura_id | alumno_id |
|----|---------------|-----------|
| 1 | 1 | 0 |
| 2 | 1 | 0 |
| 3 | 3 | 3 |