Eloquent 也会假定每个数据表都有一个名为 id
的主键字段。你可以定义一个访问权限为 protected
的 $primaryKey
属性来覆盖这个约定。
另外,Eloquent 假定主键是一个递增的整数值,这意味着在默认情况下主键会自动转换为 int
。如果希望使用非递增或者非数字的主键,则必须在模型上设置 public $incrementing = false
。如果主键不是一个整数,你应该在模型上设置 protected $keyType = string
。
如果不设置 public $incrementing = false hasOne() 里不能使用主键