Laravel 5.2 身份验证将“id”更改为“customer_id”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34673901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:00:34  来源:igfitidea点击:

Laravel 5.2 auth change 'id' to 'customer_id'

laravelauthenticationlaravel-5laravel-5.2

提问by Draity

In this topic I asked a question: Laravel 5.2 auth change 'users' table

在这个主题中,我问了一个问题:Laravel 5.2 auth change 'users' table

But now my problem has changed. By default the users table has an id. But I want a customer_id, so I changed everything but it don't work. It keeps asking for an id.

但是现在我的问题已经改变了。默认情况下,用户表有一个id. 但我想要一个customer_id,所以我改变了一切,但它不起作用。它不断要求一个id.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from klantenwhere id= 1 limit 1)

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * from klantenwhere id= 1 limit 1)

In the table klantenI have a customer_id, but it keeps asking for an id.

在表中klanten我有一个customer_id,但它一直要求一个id.

Things I've changed:

我改变的事情:

config/auth

配置/认证

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'klanten',
    ],

created a klantenmigration

创建了klanten迁移

public function up()
{
    Schema::create('klanten', function (Blueprint $table) {
        $table->increments('klant_id');
        $table->string('voornaam');
        $table->string('email')->unique();
        $table->string('password', 64);
        $table->rememberToken();
        $table->boolean('status');
        $table->timestamps();
    });
}

If I remove timestamps it keeps asking for created_atand updated_attoo.

如果我删除时间戳它一直要求created_atupdated_at太。

Controllers/Auth/AuthController

控制器/身份验证/身份验证控制器

protected function validator(array $data)
{
    return Validator::make($data, [
        'voornaam' => 'required|max:255',
        'email' => 'required|email|max:255|unique:klanten',
        'password' => 'required|confirmed|min:6',
    ]);
}

protected function create(array $data)
{
    return User::create([
        'voornaam' => $data['voornaam'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

app/User

应用程序/用户

protected $table = 'klanten';

protected $fillable = [
    'voornaam', 'email', 'password',
];

I hope somebody can help me!

我希望有人可以帮助我!

回答by ssuhat

edit your user model and add this:

编辑您的用户模型并添加以下内容:

protected $primaryKey = 'customer_id';

回答by Moppo

you can set the $primaryKeyfiled in the Usermodel to specify the primary key of your table:

您可以$primaryKeyUser模型中设置字段以指定表的主键:

protected $primaryKey = 'customer_id';