每次我创建时都有不同的增量

different increment everytime i create

所以我有这个 table。我想创建具有不同 ID 的项目,但它必须相同 table 所以我制作了 2 个不同的 ID 类型。

|--------------------------------------|
|id | idtype1 | idtype2 | Project_name |
|--------------------------------------|

如果我创建一个带有 idtype1 的项目,idtype1 会递增,但 idtype2 仍然

|--------------------------------------|
|id | idtype1 | idtype2 | Project_name |
|--------------------------------------|
| 1 |    1    |    0    |     name1    |
| 2 |    2    |    0    |     name2    |
|--------------------------------------|

但是如果我使用 idtype2 创建一个项目,它会执行相反的操作

|--------------------------------------|
|id | idtype1 | idtype2 | Project_name |
|--------------------------------------|
| 1 |    1    |    0    |     name1    |
| 2 |    2    |    0    |     name2    |
| 3 |    0    |    1    |     name3    |
| 4 |    0    |    2    |     name4    |
|--------------------------------------|

这可能吗?以及如何?

听起来您想要三列:id、idtype1 和 idtype2 都自动递增?如果是这样,您可以在迁移中使用 $table->increments('idtype1'); 定义它。如 https://laravel.com/docs/master/migrations 所述。但是,值得注意的是,所有三列将始终具有彼此相同的值,因此您可能需要重新考虑为什么需要所有三列。

idtype1 和 idtype2 非常模糊,没有任何意义。这些代表什么?如果你能用他们的名字传达他们所代表的意思,事情就会有所改善。

这些是其他table中相关记录的id吗?如果是这样,您应该使用其他 table 名称来命名它们,例如type_id 或 address_id。

您还可以考虑设置外键信息,以便数据库了解这种关系并帮助您加强完整性。

你不能在这里做AUTO_INCREMENT,因为有时 idType1 需要递增而 2 不需要。所以,在你的迁移中做:

$table->Integer('idType1');
$table->Integer('idType2');

注册新项目时,根据需要在关联栏中计算+1。

编辑#1

if (project_type == 1) {
       DB::table('projects_controller')->increment('idType1');
} elseif (project_type == 2) {
       DB::table('projects_controller')->increment('idType2');
} etc etc

希望对您有所帮助。