Change default primary key in Eloquent

前端 未结 6 450
长发绾君心
长发绾君心 2020-12-05 01:58

Can I change Eloquent model primary key.

I want to set primary key for example admin_id instead of \'id\'?

I know I can change table name for m

6条回答
  •  青春惊慌失措
    2020-12-05 02:09

    If you are wanting to use a composite key (a string)

    You need to make sure you set public $incrementing = false otherwise laravel will cast the field to an Integer, giving 0

    class User extends Model {
    
        protected $primaryKey = 'my_string_key';
        public $incrementing = false;
    
    }
    

提交回复
热议问题