filter(fn (mixed $value, mixed $key): bool => is_string($key) && $key !== '' && is_string($value) && $value !== ''); collect(config('medical-report.hospital_departments', [])) ->filter(fn (mixed $name): bool => is_string($name) && trim($name) !== '') ->each(function (string $name) use ($departmentUnitMap): void { $hospitalUnitId = null; $mappedUnitName = $departmentUnitMap->get($name); if (is_string($mappedUnitName) && $mappedUnitName !== '') { $hospitalUnitId = HospitalUnit::query() ->where('name', $mappedUnitName) ->value('id'); } Department::query()->updateOrCreate( ['code' => Str::slug(Str::ascii($name))], [ 'name' => $name, 'hospital_unit_id' => $hospitalUnitId, 'is_active' => true, ], ); }); } }