We have below table structure.
clients
-id
-name
projects
-id
-title
-client_id
project_type
-id
-name
-parent_id
project_type_cost
-id
-amount
-project_id
-project_type_id
project_type table structure, Where they have hasMany relation on self.
I've created model for each table and able to understand the relation. I'm able to understand the relation but not able to generate expected output as show below. Client amount is total project cost, Project cost is the cost of all it's project types. Also we need to add filter on client_id, project_id, project_type_id.
{
"id":1,
"name":"XYZ",
"amount":1234,
"type":"client",
"children":[
{
"id":10,
"name":"Project one",
"amount":41524.759999999995,
"type":"project",
"children":[
{
"id":1,
"type":"cost",
"name":"Design",
"amount":15514.439999999999,
"children":[
{
"id":3,
"type":"cost",
"name":"Digital Design",
"amount":7461.54,
"children":[
{
"id":5,
"type":"cost",
"name":"Web Design",
"amount":3620.45,
"children":[
]
},
{
"id":6,
"type":"cost",
"name":"App Design",
"amount":3841.09,
"children":[
]
}
]
},
{
"id":4,
"type":"cost",
"name":"Print Design",
"amount":8052.9,
"children":[
]
}
]
}
]
}
]
}
I'm new to Laravel eloquent and not able to write the query can anyone help me?