0

I need to use table q_users (users at clean laravel) with columns: qID (id), qName (name), qEmail (email), qPass (password), qEmailVerifiedAt (email_verified_at). How to make laravel use them successfully?

1
  • If you want to change your default users table for login. maybe this tutorial will help you. scotch.io/@sukelali/… Commented Jan 21, 2019 at 5:48

2 Answers 2

6

To change the table, you can do this on your model.

protected $table = 'q_users';

To change the primary key, you can do this: protected $primaryKey = 'qID' ;

To change the other fields like email, name, password, you can write a migration to rename the columns in the users table. It would look like the following:

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('name', 'qName');
    $table->renameColumn('email', 'qEmail');
    $table->renameColumn('password', 'qPassword'); 
});

Before you can do that, make sure you have the doctrine/dbal package installed. You can install it by doing the following:

composer require doctrine/dbal

Changing the email and password field can have a lot of repurcussions on Laravel's authentication system and notification system. You need to do couple of changes.

In your App\Http\Controllers\Auth\LoginController, you need to add the following method which will override the one in the AuthenticateUsers trait.

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'qEmail';
}

You also need to add the following method:

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->qPassword;
}

... and that should make the Login works well with your new fields.

Changing the email will also probably break the Password Reset Email workflow. You would need to override the following method in your User model.

/**
 * Get the e-mail address where password reset links are sent.
 *
 * @return string
 */
public function getEmailForPasswordReset()
{
    return $this->qEmail;
}

I am not entirely sure how you can override the Verification workflow, but it looks like you would need to do changes here:

/**
 * Determine if the user has verified their email address.
 *
 * @return bool
 */
public function hasVerifiedEmail()
{
    return ! is_null($this->qEmailVerifiedAt);
}

/**
 * Mark the given user's email as verified.
 *
 * @return bool
 */
public function markEmailAsVerified()
{
    return $this->forceFill([
        'qEmailVerifiedAt' => $this->freshTimestamp(),
    ])->save();
}

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    // You probably need to override with your own notification here
    // which would pull your actual email
    $this->notify(new Notifications\VerifyEmail);
}

All in all, this is a tough decision to make. I am not sure why you want to do that. However, since you want to do it, there's a simpler (although still not recommended way) to ensure everything works well with your new fields.

You can define Accessors to your modified fields. For example, you can add the following accessors in your User model.

public function getNameAttribute($value)
{
    return $this->qName; 
}

public function getEmailAttribute($value)
{
    return $this->qEmail; 
}

public function getEmailVerifiedAtAttributes($value)
{
    return $this->qEmailVerifiedAt; 
}

... and that should make it work. Any calls to $user->email will actually pull the value from $user->qEmail. Although, like I said, I wouldn't personally recommend it.

Edit: Based on your comments, if you want to change the field name in the login form, you would also need to change the validation logic. So in your LoginController you would also need to override these methods.

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required|string',
        'qPassword' => 'required|string',
    ]);
}

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'qPassword');
}
Sign up to request clarification or add additional context in comments.

9 Comments

Register works, verification too, login returns "These credentials do not match our records" after renaming email to qEmail at login.blade.php
You don't need to rename it there as well. If you decide to do that you need to override couple more methods. I will edit my answer now to illustrate this.
It requires to rename it, otherwise it sais that q email field is required
Still can't auth. At stucked step it's trying to find user with email and non-hashed password (select * from 'q_users' where 'qEmail' = '[email protected]' and 'qPass' = '112233' limit 1). At DB password is hashed.
That shouldn't happen. The validateCredentials() actually hashes the password. Did you do any other overrides?
|
1

In your models use the following codes.

protected $table = 'q_users';
protected $primaryKey = 'qID' ;
protected $timestamps = false;//only if you don't use anything like created_at or updated_at. Otherwise use the two lines below.
const CREATED_AT = 'qcreated_at';
const UPDATED_AT = 'qupdated_at';

Instead of default user table in your /database/migrations you can create a q_users table.

If you need to implement authentication then change the table and/or model name accordingly in /config/auth.php.

You don't need to define your custom qName or qPass field in your model. Generally Laravel doesn't care about that. You can manually authenticate. (I prefer manual authentication for my applications.)

If you want to use make:auth scaffold then you can change in LoginController.php like this :

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

For password field you can define it in your model like,

public function getAuthPassword()
{
    return $this->qPass;
}

About email_verified_at, as far as I know, one can't change it without customizing Laravel framework. In your /vendor/aravel/framework/src/Illuminate/Auth/MustVerifyEmail.php you can put your custom field name. But it is a bit risky. Better way is to implement your own custom verification logic. If needed you can implement the MustVerifyEmail interface.

2 Comments

Yea, got it. What about name, pass and (most wanted) email_verified_at?
I have added in my original answer. Hope it will answer your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.