Laravel:具有不同用户表名的不同列名

Edg*_*per 0 sql laravel laravel-5

我需要使用表q_users(users在干净的laravel)与列:qID(id),qName(name),qEmail(email),qPass(password),qEmailVerifiedAt(email_verified_at).如何让laravel成功使用它们?

Moz*_*mil 5

要更改表格,您可以在模型上执行此操作.

protected $table = 'q_users';
Run Code Online (Sandbox Code Playgroud)

要更改主键,您可以执行以下操作:protected $ primaryKey ='qID';

要更改其他领域,如email,name,password,你可以写一个移民在重命名列users表.它看起来如下:

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

在此之前,请确保已doctrine/dbal安装该软件包.您可以通过执行以下操作来安装它:

composer require doctrine/dbal
Run Code Online (Sandbox Code Playgroud)

更改emailpassword字段可以对Laravel的身份验证系统和通知系统进行大量重复.你需要做一些改变.

在您的中App\Http\Controllers\Auth\LoginController,您需要添加以下方法,该方法将覆盖AuthenticateUsers特征中的方法.

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

您还需要添加以下方法:

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

...这应该使登录适用于您的新字段.

更改电子邮件也可能会破坏密码重置电子邮件工作流程.您需要在User模型中覆盖以下方法.

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

我不完全确定如何覆盖Verification工作流程,但看起来您需要在此处进行更改:

/**
 * 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);
}
Run Code Online (Sandbox Code Playgroud)

总而言之,这是一个艰难的决定.我不确定你为什么要那样做.但是,既然你想要这样做,那么有一个更简单的(虽然仍然不推荐的方式)来确保一切都适合你的新领域.

您可以为修改后的字段定义访问者.例如,您可以在User模型中添加以下访问者.

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

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

public function getEmailVerifiedAtAttributes($value)
{
    return $this->qEmailVerifiedAt; 
}
Run Code Online (Sandbox Code Playgroud)

......那应该让它发挥作用.任何调用$user->email都会实际从中拉出值$user->qEmail.虽然,就像我说的,我不会亲自推荐它.

编辑:根据您的注释,如果您想更改登录表单中的字段名称,您还需要更改验证逻辑.因此,在您的工作中,LoginController您还需要覆盖这些方法.

/**
 * 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');
}
Run Code Online (Sandbox Code Playgroud)