我想写一个Laravel迁移自动增量ID作为主键。我想用另一个值而不是1开头此ID。我该怎么办?
迁移up()
功能:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('phone');
$table->rememberToken();
$table->timestamps();
});
}
添加一个ID为ID(希望为ID -1)的记录,然后将其删除。
如果添加ID为999的记录,然后将其删除,则下一条记录的ID为1000。您也可以在数据库中使用SQL身份
您可以使用2种方法
根据声明DB::statement("ALTER TABLE your_table_here SET AUTO_INCREMENT = 9999;");
通过插入行并将其删除。
DB::table('your_table_here ')->insert(['id' => 99999, ... ,'column' => 'value']);
DB::table('your_table_here ')->where('id', 99999)->delete();
希望这会有所帮助
创建迁移后,只需转到您的Mysql数据库并输入此查询
ALTER TABLE users AUTO_INCREMENT = 1000;
尝试这个?
$table->increments('id')->start_from(10000);