Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Getting the Authentication username column

I need to get the auth username that is used by the app for authentication from a User object. The column used for authentication is changing based on system/environment (can be email or username or some other column)

Is it a good idea to put that as a public property in the User model (I will add username_column to auth.php):

class User extends Authenticatable 
{
    public $username_column = config('auth.username_column')
}

or as a getter in the User model:

class User extends Authenticatable 
{
    public function getUsernameColumn()
    {
        return config('auth.username_column');
    }
}

Or something else?

0 likes
6 replies
vincent15000's avatar

You can also handle this directly in the controller used for the authentication.

1 like
Ligonsker's avatar

@vincent15000 I do that also in the Login Controller if you mean overriding the username() method. But I need to get that piece of information from the User object later as well. Or I didn't understand you right?

1 like
Ligonsker's avatar

@martinbean I checked those, but they return the unique identifier of the table (id column and its value) and not the column used to identify the user. You think that it is supposed to return the column and there is an issue with the code somewhere?

1 like
martinbean's avatar
Level 80

@Ligonsker Yes. And you can override the value in your User class to return whatever the actual value is:

class User extends Authenticatable
{
    public function getAuthIdentifierName()
    {
        return config('auth.username_column');
    }
}
2 likes
Ligonsker's avatar

@martinbean thank you! So I will do it! I wanted make sure I do something in a good practice

1 like

Please or to participate in this conversation.