classSection ->newQuery() ->whereIn('class_section_id', $sectionIds) ->get() ->each(function ($section) use (&$map) { $map[(int) $section->class_section_id] = $this->sectionName($section); }); $missing = collect($sectionIds) ->map(fn ($id) => (int) $id) ->reject(fn ($id) => array_key_exists($id, $map)) ->values() ->all(); if (! empty($missing)) { $this->classSection ->newQuery() ->whereIn('id', $missing) ->get() ->each(function ($section) use (&$map) { $map[(int) $section->id] = $this->sectionName($section); }); } return $map; } private function sectionName(ClassSection $section): string { return (string) (collect([ $section->class_section_name ?? null, $section->section_name ?? null, $section->name ?? null, ])->first(fn ($value) => trim((string) $value) !== '') ?? ''); } }