Answers.Body: <p>Query for it like</p>
  <pre><code>use craft\elements\User;
  
  $user = User::find()-&gt;username('username')-&gt;one();
  </code></pre>
  <p>You can also get the currently logged-in user like:</p>
  <pre><code>use Craft;
  
  $currentUser = Craft::$app-&gt;getUser()-&gt;getIdentity();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-22T12:03:27.773Z
Answers.LastActivityDate: 2022-07-22T13:47:57.700Z
Answers.LastEditDate: 2022-07-22T13:47:57.700Z
Answers.UserDisplayName: Ryan
Body: <p>How to query a user by username on controller level?</p>
  <p>In twig I can do:</p>
  <pre><code>{% set user = craft.users.username('username') %}
  </code></pre>
  <p>How to do that in controller?
  Thank you for your help.</p>
  
Tags: <users><controller>
Title: How to query a user by username on controller level?
ThreadText: # How to query a user by username on controller level?
  
  (Asked by: username_0 on 2022-07-22)
  
  How to query a user by username on controller level?
  
  In twig I can do:
  
  ```
  {% set user = craft.users.username('username') %}
  
  ```
  
  How to do that in controller?
  Thank you for your help.
  
  **Tags:** users, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-22. Score: 1 | Accepted answer
  
  Query for it like
  
  ```
  use craft\elements\User;
  
  $user = User::find()->username('username')->one();
  
  ```
  
  You can also get the currently logged-in user like:
  
  ```
  use Craft;
  
  $currentUser = Craft::$app->getUser()->getIdentity();
  
  ```
  
  
  ---
  
  