简体   繁体   中英

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. Example:

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

These functions always check for id attribute of users table, how to change this behavior of 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:

public $incrementing = false;

Otherwise the key will be casted to int

Eloquent assumes that the foreign key should have a value matching the id (or the custom $primaryKey )

So, in your User model use

protected $primaryKey = 'userId';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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