所以我有自定义主键,我不确定我是否需要在eloquent和migrations中定义增量?
class Question extends Model
{
protected $primaryKey = 'que_id';
protected $keyType = 'tinyInteger';
public $autoincrement = true;
}
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->tinyInteger('que_id')->autoIncrement();
$table->timestamps();
});
}
或者只是这样定义它?
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->tinyInteger('que_id')->primary();
$table->timestamps();
});
}
模型:
$keyType
支持int[eger]
和string
$autoincrement
财产,它被称为$incrementing
。移民:
tinyInteger()
等的第二个参数是$autoIncrement = false
。$autoIncrement
,则不需要也不能使用->primary()
(至少在MySQL上)。在模型中,只更改$primaryKey
:
class Question extends Model
{
protected $primaryKey = 'que_id';
}
在迁移中,使用tinyIncrements()
:
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->tinyInteger('que_id')->unsigned()->autoIncrement();
$table->timestamps();
});
}
这应该工作