Dibyaranjan Dibyaranjan - 1 month ago 5
PHP Question

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.

Answer

To override the primary key used to search you must define the primaryKey property on the User model.

protected $primaryKey = 'userId';
Comments