简体   繁体   English

3个不同表之间的Laravel关系

[英]Laravel relationships between 3 different tables

Trying to figure out the following: 尝试找出以下内容:

  1. How to set up these tables. 如何设置这些表。
  2. The relationship between the tables using eloquent. 表之间的关系使用雄辩。

Essentially, you can create a post that is tied to a task. 本质上,您可以创建与任务相关的帖子。 Each task can have unique details that would need the users input. 每个任务可以具有需要用户输入的唯一详细信息。

Tables (one way of doing this) 表(执行此操作的一种方法)

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();
});

Now I want to set this up in a way that I can grow this. 现在,我想以一种可以扩展它的方式进行设置。 As I add more tasks to the site I can dynamically grow that out. 当我向站点添加更多任务时,我可以动态地将其扩展。 Thats why I am thinking of a posts table which references a Task Table which then can go into a details table for that specific task? 那就是为什么我要考虑一个引用任务表的帖子表,然后该表可以进入该特定任务的详细信息表?

First of all is this the right way of setting this up? 首先,这是设置此方法的正确方法吗?

Second of all what or how would i tie this together using eloquent? 其次,我将如何雄辩地将其结合在一起? Many to many through? 多对多? pivot table. 数据透视表。 This is for an API so its important to be able to build the JSON object ... this is not just getting passed to a view or something like that... 这是针对API的,因此能够构建JSON对象非常重要...这不仅仅是传递给视图或类似的东西...

Database Scheme 数据库方案

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();
});

Example Code 范例程式码

$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'
]);

@dov @dov

so if i expanded on your idea i would end up with this then... no? 所以,如果我扩展您的想法,我最终会得到结论,那么...不? Sorry it still is not clicking for me how i would make this work. 对不起,它仍然没有为我点击我将如何进行这项工作。 apologies. 道歉。 I would end up with entries like: 我最终会得到像这样的条目:

DB entry for subtask 1 and sub task 2 子任务1和子任务2的DB条目

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

Tables: 表格:

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM