Laravel与父表对应的多级关系

时间:2020-10-08 12:58:47

标签: laravel

很抱歉,标题令人困惑,但让我尝试澄清一下。 我有一个学生表,然后是一个进程表,其中有一个子表任务。然后,我想为每位学生提供每个过程的相应记录,并且类似地为每个过程任务提供一个相应记录。

例如:

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

我觉得我的人际关系有问题吗?我如何获得特定学生的流程及其相关任务?

1 个答案:

答案 0 :(得分:0)

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

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

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