【问题标题】:Laravel Multi Level Relationships with Corresponding Table Relating to ParentLaravel 多级关系以及与父相关的对应表
【发布时间】:2020-10-08 12:58:47
【问题描述】:

抱歉,如果标题令人困惑,但让我试着澄清一下。 我有一个 students 表,后跟一个 processes 表,其中有一个 child 表 process tasks。然后我想,对于每个学生,每个流程都有对应的记录,同样,每个流程任务都有对应的记录。

例如:

Student ABC
Student DEF
Student GHI

Process 1: Enter Student Process
    - Process Task: Enter his name
    - Process Task: Phone the student
    - Process Task: Upload his photo

Process 2: Something else
    - Process Task: Tick this
    - Process Task: Tick that

这样我就可以为每个学生设置“待办事项列表”(流程及其相关任务),每个列表都有各自的状态。

(简化)表格

Schema::create('students', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('student_name',64);
        $table->string('student_surname',64);
        $table->timestamps();
    });

    Schema::create('processes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('process')->nullable();
        $table->timestamps();
    });

    Schema::create('process_tasks', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('process_id')->unsigned()->index('process_id_index');
        $table->string('process_task')->nullable();
        $table->integer('sequence'); // task order
        $table->timestamps();
    });

    Schema::create('process_student', function (Blueprint $table) {
        $table->bigInteger('student_id')->unsigned();
        $table->bigInteger('process_id')->unsigned();
        $table->tinyInteger('status')->unsigned()->nullable(); // eg: awaiting feedback, due, etc
        $table->timestamps();
        $table->foreign('student_id', 'ps_student_id_foreign')->references('id')->on('students')->onDelete('cascade');
        $table->foreign('process_id', 'ps_process_id_foreign')->references('id')->on('processes')->onDelete('cascade');
    });

    Schema::create('process_task_student', function (Blueprint $table) {
        $table->bigInteger('student_id')->unsigned();
        $table->bigInteger('process_task_id')->unsigned();
        $table->boolean('complete')->default(0); // eg: awaiting feedback, due, etc
        $table->timestamps();
        $table->foreign('student_id', 'spt_student_id_foreign')->references('id')->on('students')->onDelete('cascade');
        $table->foreign('process_task_id', 'spt_process_task_id_foreign')->references('id')->on('process_tasks')->onDelete('cascade');
    });

模型/关系

流程

protected $fillable = [
    'process'
];

public function tasks() {
    return $this->hasMany('App\ProcessTask');
}

处理任务

protected $fillable = [
    'process_id',
    'process_task',
    'sequence'
];

学生

protected $fillable = [
    'id','student_name','student_surname'
];

public function processes() {
    return $this->belongsToMany('App\Process')->withTimestamps();
}

public function tasks() {
    return $this->processes()->belongsToMany('App\ProcessTask')->withTimestamps();
}

//编辑:添加了进一步的代码以进行澄清

控制器:

public function show($id)
{
    $student = Student::with(['processes','processes.tasks'])->findOrFail($id);
    return view('students.show', compact('student'));
}

查看:

@foreach ($student->processes as $student_process)
@foreach ($student_process->tasks->sortBy('sequence') as $student_process_task)
    <div class="kt-widget2__item kt-widget2__item--primary">
      <div class="kt-widget2__checkbox">
        <label class="kt-checkbox kt-checkbox--solid kt-checkbox--single">
        <input type="checkbox">
        <span></span>
        </label>
      </div>
      <div class="kt-widget2__info">
        <a href="#" class="kt-widget2__title">
            {{$student_process_task->process_task}}
        </a>
      </div>
    </div>
@endforeach
@endforeach

我觉得我的人际关系出了问题?我将如何获得该流程及其对特定学生的相关任务?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    因此,学生和流程之间存在多对多关系,但流程和任务之间存在一对多关系。但是您也有一个用于流程任务关系的数据透视表。我很困惑。首先,确保添加反向关系,因为我没有看到任何关系。如果流程任务是多对多,则将 hasMany 更改为 belongsToMany。长话短说,请仔细检查您是否正确构建了所有关系。然后转到 StudentController.php 和

    public function index()
    {
      return Student::with("processes.tasks")->get();
    }
    

    在此页面https://laravel.com/docs/8.x/eloquent-relationships 中搜索“嵌套急切加载”以获取更多信息。

    【讨论】:

    • Process hasMany Tasks - 如示例所示。那就是“待办事项清单”本身,然后每个学生都有每个此类过程/任务的“副本”。例如:我需要查找“Student ABC”并查看他的status 是“Process 1: Enter Student Process”的什么以及complete 的值是“Process Task: Enter his name”为了澄清,一个 ProcessTask "belongsTo" 一个进程(因为它只属于一个进程)。
    • 我的错误。通过查看它的名称,我认为“process_task”是一个数据透视表(肌肉记忆)。但我的答案的其他部分应该是正确的。所以定义反向关系并更新你的索引函数。
    • 这并不能解决问题,因为引用 @foreach ($student-&gt;processes as $student_process) 指的是 process_student 数据透视表(正确)。但是,@foreach ($student_process-&gt;tasks-&gt;sortBy('sequence') as $student_process_task) 引用的是 tasks 表,而不是 ```process_task_student` 数据透视表?
    • 你不需要学生和任务之间的任何直接关系,所以“process_task_student”数据透视表。 Student 和 Process 模型具有多对多的关系。 Process 和 Task 模型具有一对多的关系。这意味着您可以从学生控制器通过进程访问任务。有很多方法可以做到这一点。
    • 抱歉,我没有关注 - 也许您可以更新您的答案以反映您的意思并显示控制器方面并在视图中显示用例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2019-10-16
    • 2018-06-28
    相关资源
    最近更新 更多