Eloquent model returns 0 as primary key

前端 未结 2 909
故里飘歌
故里飘歌 2021-01-03 19:57

I have this migration:

Schema::create(\'provincias\', function (Blueprint $table) {
    $table->string(\'codigo\', 2);
    $table->string(\'nombre\', 5         


        
相关标签:
2条回答
  • 2021-01-03 20:15

    You can also set the type of primary key:

    protected $keyType = 'string';
    public $incrementing = false;
    

    Important note. You should add the second line. $incrementing = false. If you do not this, the key will be 0 after calling save() on your model.

    0 讨论(0)
  • 2021-01-03 20:19

    You can try setting public $incrementing = false; on your model so eloquent doesn't expect your primary key to be an autoincrement primary key.

    0 讨论(0)
提交回复
热议问题