首页 > 解决方案 > 复杂的 laravel 雄辩查询

问题描述

我们有下表结构。

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表结构,它们与自身有很多关系。

我已经为每个表创建了模型并且能够理解关系。我能够理解这种关系,但无法生成预期的输出,如下所示。客户金额是项目总成本,项目成本是所有项目类型的成本。我们还需要在 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":[
                        
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

我是 Laravel 雄辩的新手,无法编写查询,谁能帮助我?

标签: phplaraveleloquentlaravel-8

解决方案


推荐阅读