Laravel 5.1自定义表名和字段名

时间:2015-12-17 18:31:00

标签: php mysql laravel

我正在使用已安装的数据库将旧应用程序升级到Laravel 5.1并使用开箱即用的身份验证系统(内置)。注册/登录仅在我使用电子邮件和密码作为字段名称时才有效,否则让用户注册但不允许他们登录。我的问题是我们可以使用自己的归档名称吗?

3 个答案:

答案 0 :(得分:0)

我相信您需要更改migrations& User模型的$fillable属性... AuthControllerIlluminate\Foundation\Auth\AuthenticatesUsers postLoginloginUsername方法

答案 1 :(得分:0)

您应该将username属性设置为AuthController。

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $username = 'field_name';

    ...
}

检查Illuminate\Foundation\Auth\AuthenticatesUsers@loginUsername方法。

修改

如果要更改password字段名称(或使用laravel auth中的其他字段),则应覆盖postLogin方法,因为它是硬编码的。

public function postLogin(Request $request)
{
    $this->validate($request, [
        'co_Email' => 'required', 'co_Password' => 'required'
    ]);

    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }

    $credentials = $request->only('co_Email', 'co_Password');

    if (Auth::attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }

    return redirect($this->loginPath())
        ->withInput($request->only('co_Email', 'remember'))
        ->withErrors([
            'co_Email' => $this->getFailedLoginMessage(),
        ]);
}

答案 2 :(得分:0)

Laravel让这很简单。就像@pespantelis所说,你可以覆盖电子邮件字段。在您的表格中:

<input type="email" name="co_Email">

然后,在AuthController

protected $username = 'co_Email';

为了让Laravel在仍然使用默认身份验证系统的同时查看正确的密码列,它有点不同但仍然一样容易。只需按照以下步骤操作:

  1. 不要更改密码字段的名称,只需将其命名为&#34;密码&#34;正如你通常所做的那样:

    <input type="password" name="password">
    
  2. 但是将此方法添加到您的用户模型中:

    public function getAuthPassword()
    {
        return $this->co_Password;
    }
    
  3. Laravel将比较&#34;密码&#34;字段与&#34; co_Password&#34;列。