【发布时间】: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