0

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?

  • What data do you need to retrieve? Also the data you included in your question, is it the current output or the expected output? It also don't respect the table structure you given above. – JawadR1 Feb 11 '21 at 07:33
  • It's expected json response – Laravel User Feb 11 '21 at 07:48
  • You either need a package or a tree function to get this output. Here are a few links for your reference: [SO](https://stackoverflow.com/questions/34758965/eloquent-parent-child-relationship-on-same-model) [Laracasts](https://laracasts.com/discuss/channels/eloquent/how-to-build-a-tree-structure-with-the-help-of-eloquent) – JawadR1 Feb 11 '21 at 08:02
  • Thanks for the links I'll check the solution. – Laravel User Feb 11 '21 at 10:09

0 Answers0