I am struggling to get Laravel Eloquent to retrieve and group results in the way that i'd like.
Basically I am creating a 'My Agenda' page which shows all task in order of their due date, but grouped together if 2 or more Tasks (in order) belong to the same Stage, and likewise if 2 or more stages belong to the same project.
My data is Projects -> (has many) Stages -> (has many) Tasks
I would like to output my data as follows:
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)
Any ideas how I can achieve this? I am open to doing this on the front end with JS/Vue/Lodash.
Thanks in advance!
M
I think you can do it this way:
First, let's combine all the tables with JOIN. If you want to see all projects and stages that don't have any relational data, you can use LEFT JOIN, or maybe RIGHT JOIN, I don't know which one will work.
I think you should aim for this type of array as your output, so you won't have any issues because of the repeated key names.
To create that type of array, the function down below should work.
I created those codes here directly. There can be typos and everything, but the logic should work.