【问题标题】:Laravel relationships between 3 different tables3个不同表之间的Laravel关系
【发布时间】:2016-09-03 14:02:58
【问题描述】:

试图弄清楚以下内容:

  1. 如何设置这些表。
  2. 表之间的关系使用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


【解决方案1】:

数据库方案

Schema::create('tasks', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

Schema::create('sub_tasks', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('description');
    $table->unsignedInteger('task_id');
    $table->foreign('task_id')
          ->references('id')
          ->on('tasks')
          ->onDelete('cascade');
    $table->timestamps();
});

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->unsignedInteger('task_id');
    $table->foreign('task_id')
          ->references('id')
          ->on('tasks')
          ->onDelete('cascade');
    $table->timestamps();
});

示例代码

$task1 = Task::create(['title' => 'Market Shopping']);
$task2 = Task::create(['title' => 'Spring Cleaning']);

// Subtask for Task1
$task1->subTasks()->create([
    'title'       => 'Buy shampoo',
    'description' => 'Around 3 bottles will be good'
]);

$task1->subTasks()->create([
    'title'       => 'Buy donuts',
    'description' => 'I love the sprinkled ones!'
]);

// Subtask for Task2
$task2->subTasks()->create([
    'title'       => 'Recycle old bottles',
    'description' => 'They are in the garage'
]);

$task2->subTasks()->create([
    'title'       => 'Wash car',
    'description' => 'Do not forget the back seat'
]);

【讨论】:

  • 所以唯一的问题是子任务会有不同的输入。以你的例子让我修改一下。 1 秒。
  • PHP 在假设变量类型方面非常笨拙,您应该有任何问题: ("1" == 1) in PHP
  • 看看我在下面给出的答案。
  • @Citti:当task3出现时,你打算做什么,创建另一个表?
  • 是的,这是我的想法。无论如何,它们将是自定义字段吗?这不是接近它的方法吗?
【解决方案2】:

@dov

所以如果我扩展了你的想法,我最终会得到这个......不是吗?抱歉,它仍然没有为我点击我将如何完成这项工作。道歉。我最终会得到如下条目:

子任务 1 和子任务 2 的数据库条目

1 | 1 | x | y | z | NULL | NULL | NULL | NULL | NULL | timestamps
2 | 2 | NULL | NULL | NULL | a | b | c | d | timestamps

表格:

Schema::create('tasks', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

Schema::create('sub_tasks', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('task_id');
    $table->foreign('task_id')
          ->references('id')
          ->on('tasks')
          ->onDelete('cascade');
    $table->string('task1 specific input1');
    $table->string('task1 specific input2');
    $table->string('task1 specific input3');
    $table->string('task2 specific input1');
    $table->string('task2 specific input2');
    $table->string('task2 specific input3');
    $table->string('task2 specific input4');
    $table->string('task2 specific input5');
    $table->timestamps();
});

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->unsignedInteger('task_id');
    $table->foreign('task_id')
          ->references('id')
          ->on('tasks')
          ->onDelete('cascade');
    $table->timestamps();
});

【讨论】:

  • 您不应该为输入创建列。利用关系
  • 确定我听到了键/值设置。但我将如何处理输入之间的差异......一些细节是布尔值。有些是文字。有些是int。那么我将如何在该子任务表中存储所有这些不同的细节......
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多