how to get parent tids of a taxonomy term
In $ancestors you'll find an array of the parent terms starting with the term $tid itself:

$ancestors = \Drupal::service('entity_type.manager')-&gt;getStorage("taxonomy_term")-&gt;loadAllParents($tid);
$list = [];
foreach ($ancestors as $term) {
  $list[$term-&gt;id()] = $term-&gt;label();
}