doupo5861 2014-10-12 20:18
浏览 63
已采纳

带有附加参数的laravel pivot表

I'm building a laravel app that tracks projects. A user can define tasks and attach them to projects.

I'd like the user to be able to have a list of tasks that can be re-used for multiple projects. This is simple enough using a pivot table that links projects and tasks:

public function up()
{
    Schema::create('project_task', function($table)
    {
        $table->increments('id');
        $table->integer('project_id');
        $table->integer('task_id');
        $table->timestamps();
    });
}

However, I'd like the user to be able to set different due dates for tasks depending on the project. So on project 1, Task A is due in one week. On project 2, Task A is due in a month.

What's the best table schema to store these type of values? Does there need to be another pivot table that links the task parameters to the project ID? I'm not sure how to handle this.

Thanks for your input.

EDIT: I'm using the Eloquent ORM, so the pivot rows are never really exposed, it just grabs the Task object associated with the Project.

  • 写回答

1条回答 默认 最新

  • duanliang4009 2014-10-12 20:29
    关注

    You can simply add due_date field to the project_task table. So in total you will have three tables project, task and project_task (which you have in your question)

    Like this you will define the task once and can assign different due date for each project

    Edit

    You can define a custom pivot model that includes the due_date see Defining A Custom Pivot Model

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?