Laravel: How to set the Primary Key and Foreign Key to string

后端 未结 2 404
野趣味
野趣味 2020-12-21 13:19

I\'m not using the auto increment for the id instead i\'m using the 32 char unique id. So when i create a relationship and query, im getting a null because my FK expecting i

2条回答
  •  借酒劲吻你
    2020-12-21 13:26

    In newer versions of Laravel (Im using 6.5.1) you have to set up the key type to string (By default PK type is set to integer)

    Following the example of the question:

    class User extend Eloquent {
        public $incrementing = false; 
        public $keyType = 'string';
    }
    
    class Reservation extend Eloquent {
        public $incrementing = false;
        public $keyType = 'string'; 
    }
    

    You can see the example in the laravel documentation

提交回复
热议问题