<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>

CC BY-SA 4.0
2022-07-22T12:03:27.773Z
2022-07-22T13:47:57.700Z
2022-07-22T13:47:57.700Z
Ryan
<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>

<users><controller>
How to query a user by username on controller level?
# 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();


---

