php - Laravel - Save Many to Many, how to combine two arrays in a foreach
one text
Solution:
You can do it this way:
foreach($scores as $index => $score) {
$courses->users()->attach($userid, ['score' => $score, 'course_id' => $courses[$index]]);
}
Pay attention that $courses
and $scores
both need to be arrays. Double check if this line $courses = new Course(request(['course_id']));
actually creates $courses
array.
Hope it helps.
Source