students->each(function ($item, $key) use ($data, $lesson) { if ($data['score' . $item->id]) { $lesson->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]); } }); return $lesson; } }