Get Primary Key Field Name of a model

Hi all,

How to get the primary key field name of model? The document said the primaryKey() method ‘The default implementation simply returns null, meaning using the primary key defined in the database.’ if not overridden.

Does the following code work?

$attributeNames = $model->attributeNames();

$pkFieldName = $attributeNames[0];


Try this:


1 Like

It’s the correct way to do it. Thanks a lot.