I have the following tables
**estudiante**
- id
- nombre
**grupo**
-id
-nombre
**estudiante_grupo**
-id
-grupoId
-estudianteId
So, I want to register in the group table , and then register in student_group with the student id's as an array. and the last generated id of the group table . I can do it using "query builders" but I would like to do it and save code with eloquent. another question, would it be necessary to create a model for the student_group detail table
What I have done (and working):
$lastId = DB::table("grupo") -> insertGetId(
[
"nombre" => $request -> nombre
]);
foreach($request -> estudiantesIds as $estId) {
DB::table("estudiante_grupo") -> insert([
"grupoId" => $lastId,
"estudianteId" => $estId
])
}
The array $request -> studentIds are numeric values:
[1,2... ,n]
Your scenario is a Many-to-Many relationship
Model configuration
Your models:
Where originally they should be like this:
estudiante_id
grupo_id
In addition, as the tables related to your models are in the singular, they do not follow the naming convention, therefore you must add said value in a property
Student Model
Group Model
The arguments that you are going to pass to the method from the second to the fourth position will be added depending on whether or not you follow Laravel conventions and are:
Register registration for both models
At this point you should have already registered both the user and the group with a syntax similar to this:
Y
The above is the shortest and most functional syntax possible with which you are going to register a new record for each of the models; to make use of
$request->all()
is to take all the values that the user sends and register them where they correspond.It is important to remember that in order to use the method
create
you must indicate in the property$fillable
which values will allow mass assignment.Get ids and register them in the pivot table
Now we need to find the id of the user and the id of the group that their record will be attached to.
Now we will make an
attach
id grThe above should be adding to your table the student id and the group id
REFERENCES
attach
In this answer, I have assumed several things, that the relationship is many to many and that in the intermediate table courseId you have made a mistake and it would be studentId.
Assuming that you have created the Group and Student models for the groups and students tables, respectively.
To insert an array of student ids that you receive by parameter to the newly created group, it would be like this:
With
Eloquent
you can use the methods:sync: refresca relacion en tabla Pivote
attach: agrega relacion a la tabla pivote
But you need to define the relationships
M <-> M
in the respective models ofGrupo
andEstudiante
Save and Update Methods
Before using
attach
we must save theGrupo
so that it can take theid
from it, you can use normalsync
if you want, and with this method you forget aboutfor
anddettach
, since the tablepivote
has no relevant data, and you don't need to create itModelo