How To Change The Default Primary Key in Laravel Eloquent

By default, Laravel’s eloquent presumes that each table in the database has a primary key column with name id. We can easily override this behaviour by adding a single line of code to your Model.

Quick Tip : How to change the redirect URL when logging out?

To define a custom primary key create a $primaryKey property.

/**
* primaryKey
*
* @var string
* @access protected
*/
protected $primaryKey = 'user_id';

 

2 Comments

  1. 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

    Reply

Leave a Comment.