更改 Laravel 身份验证表名和列名

Sav*_*age 7 laravel laravel-5 laravel-authorization laravel-authentication

我想更改laravel auth 表的表名和一些列名。

  • 将表名从“用户”更改为“帐户”
  • 将表列名称从“名称”更改为“用户名”
  • 将表列名称从“email”更改为“email_addr”
  • 将表列名从“updated_at”更改为“last_updated_at”

我要采取哪些步骤或编辑哪些代码而不破坏某些内容?

我以前试过这个,注册成功了,但登录没有。每当我尝试登录时,我都会被重定向回登录页面。

小智 10

您可以按照以下给定的步骤操作:

  1. 创建/修改迁移以将用户表更改为帐户
  2. 创建迁移以根据您对表帐户的要求更改列名称。确保此模型调用是扩展Authenticatable
  3. 帐户表创建模型类。
  4. 确保添加表格的可填充隐藏属性。
  5. 现在检查login.blade.php文件并将email输入文本字段名称更改为email_address.

通过以上所有步骤,我们已经准备好View部分,现在让我们开始自定义Auth

  1. 现在打开config/auth.php

    • 从数组更改 'model' => App\User::class,'model' => App\Account:class内部providers数组。
  2. 现在我们需要在app/Http/Auth/LoginController.php 中添加新函数,如下所示:

public function username(){ return 'email_address'; // this string is column of accounts table which we are going use for login }

现在我们完成了所有调整,您可以测试功能。

我已经测试了它的功能和它的魅力:)


Sur*_*wat 5

您必须在您的帐户模型中扩展“Illuminate\Foundation\Auth\User”。

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Account extends Authenticatable
{ 
  use Notifiable;

  //code here
  public function getEmailAttribute() {
      return $this->email_addr;
  }

  public function setEmailAttribute($value)
  {
    $this->attributes['email_addr'] = strtolower($value);
  }
}
Run Code Online (Sandbox Code Playgroud)

并在提供者数组中的“config/auth.php”中更改配置文件

'users' => [
        'driver' => 'eloquent',
        'model' => App\Account::class,  //replace User to Account
    ],
Run Code Online (Sandbox Code Playgroud)