如何在 Laravel 中存储一些标签,以及如何在技术上使用相同的 ID 存储这些标签
How to store some tags in Laravel and how can I technically store those tags using the same ID
我的问题是在迁移中使用 ID 或某个整数是否可以,以及如何在控制器中使用相同的 ID 存储多个标签?
这样存储(在同一个ID上存储多个标签):
身份标签
1 LARAVEL
1 PHP
Schema::create('table_test', function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->string("tags")
});
我不确定为什么要在 table 中保存具有相同 ID 的标签,但从技术上讲我们可以做到这一点。
为每次插入创建一个唯一的id。我建议你使用 uuid
Schema::create('tags', function (Blueprint $table) {
$table->uuid('id');
$table->string('tags');
});
每次插入时,先生成uuid
// Generate uuid
$uuid = Str::uuid();
// Insert to DB
DB::table('tags')->insert([
['id' => $uuid, 'tags' => 'Laravel'],
['id' => $uuid, 'tags' => 'PHP']
]);
我的问题是在迁移中使用 ID 或某个整数是否可以,以及如何在控制器中使用相同的 ID 存储多个标签?
这样存储(在同一个ID上存储多个标签):
身份标签
1 LARAVEL
1 PHP
Schema::create('table_test', function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->string("tags")
});
我不确定为什么要在 table 中保存具有相同 ID 的标签,但从技术上讲我们可以做到这一点。
为每次插入创建一个唯一的id。我建议你使用 uuid
Schema::create('tags', function (Blueprint $table) {
$table->uuid('id');
$table->string('tags');
});
每次插入时,先生成uuid
// Generate uuid
$uuid = Str::uuid();
// Insert to DB
DB::table('tags')->insert([
['id' => $uuid, 'tags' => 'Laravel'],
['id' => $uuid, 'tags' => 'PHP']
]);