Answers.Body: <p>I used August's answer, but fixed it:</p>
  
  <p>Put this at the top:</p>
  
  <pre><code>use craft\elements\User;
  </code></pre>
  
  <p>Then the updating code</p>
  
  <pre><code>$myUser = Craft::$app-&gt;getUser()-&gt;getIdentity();
  $myUser-&gt;setFieldValue('fieldHand', 'value');
  
  Craft::$app-&gt;getElements()-&gt;saveElement($myUser, false);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-29T10:41:16.527Z
Answers.LastActivityDate: 2018-11-29T10:41:16.527Z
Answers.UserDisplayName: Tim Coysh
Answers.Body: <p>Be sure and stash a reference to the User element you want to edit:</p>
  
  <pre><code>$myUser = Craft::$app-&gt;getUser()-&gt;getIdentity();
  $myUser-&gt;setFieldValue('myUserField', 'New Field Value');
  
  if (Craft::$app-&gt;getElements()-&gt;saveElement($myUser)) {
      echo "User saved.";
  }
  </code></pre>
  
  <p>Otherwise, you may end up repeatedly fetching the stale record out of the database, instead of saving the modified instance.</p>
  
  <blockquote>
    <p>Note: The similarly-named <code>Craft::$app-&gt;getUsers()</code> is a method for getting the Users Service instance, which is responsible for saving users. Not to be confused with <code>Craft::$app-&gt;getUser()</code>, which is a (Yii) method for fetching the current user (the subsequent call to <code>getIdentity()</code> retrieves the Craft Element for the user).</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-29T06:47:45.020Z
Answers.Text: I get this error now - Calling unknown method: craft\services\Users::saveUser()
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-29T23:21:48.583Z
Answers.Text: @DavidAaronMcInnis Sorry! Updated the answer—took a bad guess at the method, but it turns out you save Users just like any other Element, via the Elements service.
Answers.UserDisplayName: August Miller
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-28T18:56:59.490Z
Answers.LastActivityDate: 2019-04-13T23:27:55.907Z
Answers.LastEditDate: 2019-04-13T23:27:55.907Z
Answers.UserDisplayName: August Miller
Body: <p>I have a custom user field 'myuserfield'</p>
  
  <p>How do I save a value to this field via a plugin in Craft3?</p>
  
  <p>Here is my current attempt.</p>
  
  <pre><code>    $myvar = '12345';
      Craft::$app-&gt;getUser()-&gt;getIdentity()-&gt;setFieldValue('myuserfield', $myvar);
      $myid = Craft::$app-&gt;getUser()-&gt;getIdentity()-&gt;getFieldValue('myuserfield');
  </code></pre>
  
  <p>When I echo $myid it does, in fact, give me the new value ($myvar or 12345) but when I check the control panel the value is not saved.</p>
  
Tags: <plugin-development><craft3><usermodel>
Title: Saving Custom Field Values to User Profile
ThreadText: # Saving Custom Field Values to User Profile
  
  (Asked by: username_0 on 2018-11-27)
  
  I have a custom user field 'myuserfield'
  
  How do I save a value to this field via a plugin in Craft3?
  
  Here is my current attempt.
  
  ```
      $myvar = '12345';
      Craft::$app->getUser()->getIdentity()->setFieldValue('myuserfield', $myvar);
      $myid = Craft::$app->getUser()->getIdentity()->getFieldValue('myuserfield');
  
  ```
  
  When I echo $myid it does, in fact, give me the new value ($myvar or 12345) but when I check the control panel the value is not saved.
  
  **Tags:** plugin-development, craft3, usermodel
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-11-29. Score: 3 | Accepted answer
  
  I used username_2's answer, but fixed it:
  
  Put this at the top:
  
  ```
  use craft\elements\User;
  
  ```
  
  Then the updating code
  
  ```
  $myUser = Craft::$app->getUser()->getIdentity();
  $myUser->setFieldValue('fieldHand', 'value');
  
  Craft::$app->getElements()->saveElement($myUser, false);
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2018-11-28. Score: 1
  
  Be sure and stash a reference to the User element you want to edit:
  
  ```
  $myUser = Craft::$app->getUser()->getIdentity();
  $myUser->setFieldValue('myUserField', 'New Field Value');
  
  if (Craft::$app->getElements()->saveElement($myUser)) {
      echo "User saved.";
  }
  
  ```
  
  Otherwise, you may end up repeatedly fetching the stale record out of the database, instead of saving the modified instance.
  
  > Note: The similarly-named `Craft::$app->getUsers()` is a method for getting the Users Service instance, which is responsible for saving users. Not to be confused with `Craft::$app->getUser()`, which is a (Yii) method for fetching the current user (the subsequent call to `getIdentity()` retrieves the Craft Element for the user).
  
  > **Comments:**
  > **username_0**: I get this error now - Calling unknown method: craft\services\Users::saveUser()  
  > **username_2**: @DavidAaronMcInnis Sorry! Updated the answer—took a bad guess at the method, but it turns out you save Users just like any other Element, via the Elements service.  
  
  ---
  
  