简体   繁体   English

Laravel更改users表的默认主键

[英]Laravel change default primary key of users table

I am trying to change the default primary key from id of users table to userId , but Laravel authentication mechanism always checks for id from users table. 我试图将默认主键从users表的id更改为userId ,但Laravel身份验证机制始终检查来自users表的id Example: 例:

Users::check() and Users::attempt() Users :: check()和Users :: attempt()

These functions always check for id attribute of users table, how to change this behavior of Laravel. 这些函数总是检查users表的id属性,如何改变Laravel的这种行为。

要覆盖用于搜索的主键,必须在User模型上定义primaryKey属性。

protected $primaryKey = 'userId';

You can use 您可以使用

protected $primaryKey = 'Your_Primary_key';

In the Models. 在模型中。

Probably worth pointing that if the primaryKey you define is not an an integer key, you also need to add these: 可能值得指出的是,如果您定义的primaryKey不是整数键,您还需要添加以下内容:

public $incrementing = false;

Otherwise the key will be casted to int 否则密钥将被转换为int

Eloquent assumes that the foreign key should have a value matching the id (or the custom $primaryKey ) Eloquent假设外键应具有与id匹配的值(或自定义$primaryKey

So, in your User model use 因此,在您的User模型中使用

protected $primaryKey = 'userId';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM