Hi @mephisto really not understand you question. Here
foreach(Student::all() as $student) {
you ger all the students, and here
$subjects = Subject::all()->pluck('id');
you get all the subjects, and then
$student->subjects()->attach($subjects);
attach all the subjects step by step to every student. So, in total, of course, all students have all subjects.
you need to specify The exact student and exact subject you want to attach, like
Students::first()->subjects(Subject::first()->id);
this example will attach first subject to first user. Or if you have specified student id and subject id
Student::find($sudentId)->subjects()->attach(Subject::find($subjectId)->id);