How come I cannot fetch the name of the taxonomy term from term id? We are doing the following query below to fetch all the taxonomy term values in the term_category vocabulary term. When we do the following query below, we get NULL value returned. 

Content Type:


The Article content type has a field_category that is set to this new term_category taxonomy term. 
Currently no Article nodes have any value populated for the field_category field as it is optional. 


Taxonomy Term:


machine name = term_category
Label = Category
no fields other than title and description (which are out of the box)
has 15 values already added to the term (such as Cloud, IT, Security)


Goal:


Using the tid from the $term_categories result set, iterate through each one to get the name of the Taxonomy term. 


How come I cannot get the name of the taxonomy term value from term id?

$db = \Drupal\Core\Database\Database::getConnection();
$term_categories = $db-&gt;query('select tid, vid from taxonomy_term_data where vid ='\'term_category\'');

foreach($term_categories as $category) {
  $category_term = (\Drupal\taxonomy\Entity\Term::load($category-&gt;tid));
  ksm($category_term-&gt;label()); // returns NULL.
  ksm($category_term-&gt;getName()); // returns NULL. 

  // Goal. 
  ksm($category_term-&gt;getName()); // Returns Cloud
}


Output of ksm($categor_term) -&gt; content
Currently your problem is that you're trying to call entity class methods on a stdClass object - directly querying the database means you're not using the API any more, the results you get aren't entities.

Unless there's a particular reason to do so, though, you shouldn't have to do it that way.

For example, the same query can be executed like this through the API:

$terms = \Drupal::entityTypeManager()
  -&gt;getStorage('taxonomy_term')
  -&gt;loadByProperties(['vid' =&gt; 'term_category']);

foreach ($terms as $term) {
  $name = $term-&gt;getName();
}


It's quicker (loading in one go, not single loads in a loop), fewer lines of code, and when you bring caching into it, probably more performant.