How to get the node id the comment is attached to in hook_comment_insert?
the correct way to load nid is,

$entity-&gt;get('entity_id')-&gt;target_id

and not

$entity-&gt;get('entity_id')-&gt;value

In case of doubt which key to use,

try,

$entity-&gt;get('entity_id')-&gt;getValue()

and inspect the returned value.