How to get tid on behalf of node id (nid) in Drupal 8? I am new in Drupal 8. I have created a node with category (taxonomy). Now I don't understand how to get tid by using nid. I am using $node-&gt;body-&gt;value to get the body field value and use {{ body }}. But to get category id if I use $node-&gt;field_category-&gt;value, it will display Null in debugging mode.

function THEME_preprocess_node(&amp;$variables) {
  $node = \Drupal::routeMatch()-&gt;getParameter('node');
  $variables['body'] = $node-&gt;body-&gt;value;    // Working fine
  $cat_id = $node-&gt;field_category-&gt;value;    // Its showing null
  kint($cat_id);
}


So can anyone please tell me how can I get the category id (tid) of a node?
A reference field doesn't have a property value. You can use entity to load the term or target_id to get the term id:

$tid = $node-&gt;field_tags-&gt;target_id;