5

I have my own users table which I want to use in auth, but the problem is the email field is "userEmail" instead of "email".

For login page I overided the below method in LoginController.php

public function username()
{
    return 'userEmail';
}

and changed the name in input field.

<input id="userEmail" type="email" name="userEmail" value="{{ old('userEmail') }}" class="{{ $errors->has('userEmail') ? 'is-invalid' : '' }}">

this is working fine for login, Need solution for Forgot Password and Reset Password forms as they are not working.

So any solution for it in Laravel 5.6

Asif Rao
  • 199
  • 2
  • 15

3 Answers3

3

You could add accessor and mutator methods to the user model:

public function getEmailAttribute() {
    return $this->attributes['userEmail'];
}

public function setEmailAttribute($value) {
    $this->attributes['userEmail'] = $value;
}

Docs here

DigitalDrifter
  • 15,855
  • 3
  • 35
  • 47
0

I think you can leverage on Laravel Mutators:

https://laravel.com/docs/5.6/eloquent-mutators

Haven't got the time to test the following but should be what you're looking for:

class User extends Model
{
    /**
     * Get the user's email.
     *
     * @param  string  $value
     * @return string
     */
    public function getEmailAttribute($value)
    {
        return $this->userEmail;
    }
}
koalaok
  • 3,414
  • 7
  • 30
  • 67
0

To save modifying php code (and breaking something), it might be easier creating a sql view of your users table and map the actual field names to the names the User class is using e.g.

CREATE VIEW v_user_auth AS
SELECT user_id AS id
, user_created_time AS created_at
, user_updated_time AS updated_at
, user_name AS name
, user_email AS email
, user_password AS password
, user_remember_token AS remember_token
FROM t_user

In the User.class add the following to get it to use the view:

protected $table = 'v_user_auth';