Laravel三个不同表格之间的关系

时间:2016-05-09 03:09:18

标签: mysql api laravel eloquent

试图找出以下内容:

  1. 如何设置这些表。
  2. 使用雄辩的表之间的关系。
  3. 基本上,您可以创建与任务相关联的帖子。每个任务都可以包含需要用户输入的唯一详细信息。

    表格(执行此操作的一种方式)

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

    现在我想以一种我可以成长的方式来设置它。当我向网站添加更多任务时,我可以动态地将其扩展。这就是为什么我想到一个引用一个任务表的帖子表,然后可以进入该特定任务的详细信息表?

    首先,这是正确的设置方法吗?

    其次,我会用滔滔不绝的方式将这种方式联系起来?通过多对多?数据透视表。这是一个API,所以能够构建JSON对象很重要...这不仅仅是传递给视图或类似的东西......

2 个答案:

答案 0 :(得分:0)

数据库计划

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 :(得分:-1)

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