laravel的数据库在添加自增的时候,会自动被设置为主键,如果要添加其他列为主键的话,步骤就比较麻烦了。
Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('email')->unique(); $table->timestamps(); $table->index('id'); //自增 只允许主键或者index $table->dropPrimary('id'); //删除老的主键 $table->primary(['id', 'email']); //增加新的主键 $table->dropIndex(['id']); //移除索引 });