$courses */ public function handle(Member $member, Collection $courses): void { if (!$member->hasNami) { return; } foreach ($courses as $course) { $member->courses()->updateOrCreate(['nami_id' => $course->id], [ 'course_id' => Course::nami($course->courseId)->id, 'organizer' => $course->organizer, 'nami_id' => $course->id, 'completed_at' => $course->completedAt, 'event_name' => $course->eventName, ]); } $courseIds = $courses->map(fn ($course) => $course->id)->toArray(); $member->courses()->whereNotIn('nami_id', $courseIds)->whereNotNull('nami_id')->delete(); } }