【发布时间】:2016-09-03 14:02:58
【问题描述】:
试图弄清楚以下内容:
- 如何设置这些表。
- 表之间的关系使用eloquent。
基本上,您可以创建与任务相关的帖子。每个任务都可以有需要用户输入的独特细节。
表格(一种方法)
Schema::create('uniqueTopic1', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->foreign('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
$table->string('uniqueDetails1');
$table->string('uniqueDetails2');
$table->string('uniqueDetails3');
$table->string('uniqueDetails4');
$table->string('uniqueDetails5');
$table->string('uniqueDetails6');
$table->string('title');
$table->string('description');
$table->string('mission');
$table->int('money');
$table->timestamps();
});
Schema::create('uniqueTopic2', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->foreign('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
$table->string('uniqueDetails1');
$table->string('uniqueDetails2');
$table->string('uniqueDetails3');
$table->int('level');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
现在我想以一种我可以发展它的方式来设置它。当我向站点添加更多任务时,我可以动态地扩展它。这就是为什么我正在考虑一个引用任务表的帖子表,然后可以进入该特定任务的详细信息表?
首先,这是正确的设置方式吗?
其次,我将如何使用 eloquent 将它们联系在一起?多对多通过?数据透视表。这是针对 API 的,因此能够构建 JSON 对象很重要……这不仅仅是传递给视图或类似的东西……
【问题讨论】:
-
为什么要为每项任务创建一个单独的表?
-
如果它们在同一个表中,我最终会得到一堆 NULL ......并且随着“任务”(我可能不是最好的名字作为例子)的数量增加,我会不想更改包含所有任务的表。任务的详细信息特定于“任务”或您想要调用的任何内容。否则你会如何设计桌子?
-
你可以使用键值对来达到同样的效果,比如 param_name(varchar 128), param_value (varchar(128))
标签: mysql api laravel eloquent