0
votes

Laravel error when I change the email field from database to user_email

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from ns_users where email = [email protected] limit 1)

Schema::create('ns_users', function (Blueprint $table) {
        $table->increments('user_id');
        $table->boolean('active')->default(0);/*by default the user is not active*/
        $table->integer('role')->default(0);/* role 0 means user and role 1 means admin!! */
        $table->string('name');
        $table->string('user_email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
});

Does anybody know what I have to change in default auth to work with user_email field instead of email?

3
you need to override default auth login functionality to work with user_email field.Ritesh Khatri
go through this file vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php here you can find username() function just copy that and paste into your auth login controller as per laravel version it may b auth controller or login controller. public function username() { return 'email'; } paste this code into it and change return value to user_email.Ritesh Khatri

3 Answers

6
votes

By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:

public function username()
{
    return 'user_email';
}
1
votes
public function getEmailAttribute()
    {
        return $this->user_email;
    }

works for me

0
votes

In case anyone of you is manually handling the laravel auth using attempt method. All you need to do is to change key name 'email' to the one you desired in associative array you are passing to the attempt method.

Auth::attempt(['user_email' => $email, 'password' => $password], $remember)