Laravel雄辩的群体结果(按关系)
我正在努力让Laravel以我喜欢的方式雄辩地检索和分组结果 基本上,我正在创建一个“我的议程”页面,该页面按任务到期日的顺序显示所有任务,但如果两个或多个任务(按顺序)属于同一阶段,或者如果两个或多个阶段属于同一项目,则将其分组 我的数据是项目->(有许多)阶段->(有许多)任务 我想将我的数据输出如下:Laravel雄辩的群体结果(按关系),laravel,eloquent,one-to-many,eloquent-relationship,Laravel,Eloquent,One To Many,Eloquent Relationship,我正在努力让Laravel以我喜欢的方式雄辩地检索和分组结果 基本上,我正在创建一个“我的议程”页面,该页面按任务到期日的顺序显示所有任务,但如果两个或多个任务(按顺序)属于同一阶段,或者如果两个或多个阶段属于同一项目,则将其分组 我的数据是项目->(有许多)阶段->(有许多)任务 我想将我的数据输出如下: Project B Stage 2 Task 1 (due 1st Sep) Task 3 (due 2nd Sep) Stage 1
Project B
Stage 2
Task 1 (due 1st Sep)
Task 3 (due 2nd Sep)
Stage 1
Task 2 (due 3rd Sep)
Project A
Stage 1
Task 2 (due 4th Sep)
Project B <---- repeated as Project A has a stage->task due before these tasks due
Stage 3
Task 2 (due 5th Sep)
Project A <---- repeated as Project B has a stage->task due before
Stage 1 <---- repeated
Task 1 (due 6th Sep)
项目B
第二阶段
任务1(9月1日到期)
任务3(9月2日到期)
第一阶段
任务2(9月3日到期)
项目A
第一阶段
任务2(9月4日到期)
项目B任务在这些任务到期之前到期
第三阶段
任务2(9月5日到期)
提前完成一项任务
第一阶段我认为你可以这样做:
首先,让我们使用JOIN组合所有表。如果您想查看所有没有任何关系数据的项目和阶段,可以使用左连接,或者右连接,我不知道哪一个可以工作
$tasks = Task::orderBy("due")
->join("stages", "stages.id", "=", task.stage_id)
->join("projects", "projects.id", "=", stages.project_id)
->select("pick the columns you want to have")
->get();
我认为应该将这种类型的数组作为输出,这样就不会因为重复的键名而出现任何问题
/*
$output = [
[
'project'=> A,
'stages'=> [
stage_name => [task 1, task 2],
stage_name => [task 4, task 8],
],
],
[
'project'=> B,
'stages'=> [
stage_name => [task 5],
],
],
[...]
];
*/
要创建这种类型的数组,下面的函数应该可以工作
$output = [];
foreach($tasks => $task) {
$project = $task['project_name'];
$lastEntry = $output[count($output) - 1];
if ( count($output) > 0 && $lastEntry['project'] == $project) {
// this means $task should be inserted in the last array.
// You should check for stages.
if (array_key_exists($task['stage_name'], $lastEntry['stages'])) {
$lastEntry['stages'][$task['stage_name']][] = $task;
} else {
$lastEntry['stages'][$task['stage_name']] = [$task];
}
// I think $lastEntry['stages'][$task['stage_name']][] = $task; will work without checking stage names, but I can't be sure, you need to try it.
} else {
// This means you should create a new item in $output.
$output[] = [
'project' => name,
'stages' => [
'stage_name' => [$task];
]
]
}
}
我直接在这里创建了这些代码。可能会有打字错误,但逻辑应该是正确的。你不能用关系来做这件事,你必须使用leftjoin和GroupBy。有什么想法或资源可以帮我解决这个问题吗?谢谢,行得通!我不得不稍微修改一下,但它把我引向了正确的方向!非常感谢。
$output = [];
foreach($tasks => $task) {
$project = $task['project_name'];
$lastEntry = $output[count($output) - 1];
if ( count($output) > 0 && $lastEntry['project'] == $project) {
// this means $task should be inserted in the last array.
// You should check for stages.
if (array_key_exists($task['stage_name'], $lastEntry['stages'])) {
$lastEntry['stages'][$task['stage_name']][] = $task;
} else {
$lastEntry['stages'][$task['stage_name']] = [$task];
}
// I think $lastEntry['stages'][$task['stage_name']][] = $task; will work without checking stage names, but I can't be sure, you need to try it.
} else {
// This means you should create a new item in $output.
$output[] = [
'project' => name,
'stages' => [
'stage_name' => [$task];
]
]
}
}