Get the user ID for a newly created user account I'm trying to import user data from a JSON file. So far I've managed to import user entities with some fields. The next step would be to retrieve the user ID.
The code I wrote is the following one.
$users_json = file_get_contents(&quot;../import/users.json&quot;);
$users_array = json_decode($users_json, true);

foreach ($users_array as $user) {
  if(!empty($user[&quot;roles&quot;])) {
    $values = array(
      'name' =&gt; $user[&quot;name&quot;],
      'pass' =&gt; '',
      'status' =&gt; $user[&quot;status&quot;],
      'mail' =&gt; $user[&quot;e_mail&quot;],
      'field_first_name' =&gt; $user[&quot;first_name&quot;],
      'field_last_name' =&gt; $user[&quot;last_name&quot;],
      'field_nationality' =&gt; $user[&quot;nationality&quot;],
      'field_introduction' =&gt; $user[&quot;introduction&quot;],
      'field_about_me' =&gt; $user[&quot;about_me&quot;],
      'field_current_job' =&gt; $user[&quot;current_job&quot;],
      'field_website' =&gt; $user[&quot;website&quot;],
    );

    $storage = \Drupal::entityTypeManager()-&gt;getStorage('user');
    $userEntity = $storage-&gt;create($values);
    if ($userEntity-&gt;save() == 1) {
      echo &quot;User &quot; . $userEntity-&gt;id . &quot; saved!\n\n&quot;;
    }
    else {
      echo &quot;User &quot; . $userEntity-&gt;id . &quot; updated!\n\n&quot;;
    }
  }
}

However, when I try to print the user ID, I get no data.
I've just started to use Drupal API, so I'm not very familiar with it. Is there something I'm doing wrong?
You don't have access to an id property. You need to use the id() method instead, e.g. $userEntity-&gt;id(). See the documentation for User.
For example:
echo &quot;User &quot; . $userEntity-&gt;id() . &quot; saved!\n\n&quot;;