Laravel采摘方法无法正确格式化对象

时间:2019-04-06 19:23:38

标签: php laravel laravel-5

我必须在OneToMany冲突模型中进行建模,并且我已经执行了ORM查询以将结果嵌套在这样的嵌套结构中:$u->userGroups()->with('space')->get(),并且得到了以下结果:

Illuminate\Database\Eloquent\Collection {#3158                            
  all: [                                                                  
    App\UserGroup {#3142                                                  
      role: "CONTRIBUTORS",                                               
      updated_at: "2019-04-06 16:38:43",                                  
      name: "Contributeurs",                                              
      created_at: "2019-04-06 16:38:43",                                  
      slug: "contributeurs",                                              
      id: 789,                                                            
      space: App\Space {#3180                                             
        country: "Congo-Kinshasa",                                        
        address: "40 rue Zozo",                  
        visibility: "public",                                             
        town: "Kinshasa",                                                 
        updated_at: "2019-04-06 16:38:42",                                
        name: "Christian Lisangola",                                      
        logo: "spaces/48fcacca888f0b35235b9edda371554dcfc2afc0.png",      
        created_at: "2019-04-06 16:38:42",                                
        zip_code: "5767",                                                 
        slug: "christian-lisangola",                                      
        id: 850,                                                          
      },                                                                  
    },                                                                    
    App\UserGroup {#3145                                                  
      role: "AUTHORS",                                                    
      updated_at: "2019-04-06 16:38:43",                                  
      name: "Auteurs",                                                    
      created_at: "2019-04-06 16:38:43",                                  
      slug: "auteurs",                                                    
      id: 788,                                                            
      space: App\Space {#3185                                             
        country: "Congo-Kinshasa",                                        
        address: "40 rue Luiss",                  
        visibility: "public",                                             
        town: "Kinshasa",                                                 
        updated_at: "2019-04-06 16:38:42",                                
        name: "Christian Lisangola",                                      
        logo: "spaces/48fcacca888f0b35235b9edda371554dcfc2afc0.png",      
        created_at: "2019-04-06 16:38:42",                                
        zip_code: "5767",                                                 
        slug: "christian-lisangola",                                      
        id: 850,                                                          
      },                                                                  
    },                                                                    
    App\UserGroup {#3148                                                  
      role: "ADMINISTRATORS",                                             
      updated_at: "2019-04-06 16:37:37",                                  
      name: "Administrateurs",                                            
      created_at: "2019-04-06 16:37:37",                                  
      slug: "administrateurs",                                            
      id: 848,                                                            
      space: App\Space {#3170                                             
        country: "États-Unis",                                            
        address: "100 avenue  zazi",                                   
        town: "Houston",                                                  
        visibility: "on_demand",                                          
        updated_at: "2019-04-06 16:37:37",                                
        name: "US",                                                       
        logo: "spaces/8490c3a93d32951d3d8d0ade7c459c70b202c342.png",      
        created_at: "2019-04-06 16:37:37",                                
        zip_code: "33234",                                                
        slug: "us",                                                       
        id: 766,                                                          
      },                                                                  
    },                                                                    
    App\UserGroup {#3151                                                  
      role: "ADMINISTRATORS",                                             
      updated_at: "2019-04-06 16:37:11",                                  
      name: "Administrateurs",                                            
      created_at: "2019-04-06 16:37:11",                                  
      slug: "administrateurs",                                            
      id: 765,                                                            
      space: App\Space {#3175                                             
        country: "États-Unis",                                            
        address: "100 avenue zozo",                                   
        town: "Houston",                                                  
        visibility: "private",                                            
        updated_at: "2019-04-06 16:37:11",                                
        name: "Lims INC",                                                
        logo: "spaces/4f1c5ac285bbc38919133b83de58ac1f7802fea4.png",      
        created_at: "2019-04-06 16:37:11",                                
        type: "private",                                                  
        zip_code: "343",                                                  
        slug: "lims-inc",                                                
        id: 844,                                                          
      },                                                                  
    },                                                                    
    App\UserGroup {#3154                                                  
      role: "ADMINISTRATORS",                                             
      updated_at: "2019-04-06 16:36:33",                                  
      name: "Administrateurs",                                            
      created_at: "2019-04-06 16:36:33",                                  
      slug: "administrateurs",                                            
      id: 1,                                                              
      space: App\Space {#3165                                             
        country: "États-Unis",                                            
        address: "100 avenue zozo",                                   
        visibility: "public",                                             
        town: "Houston",                                                  
        updated_at: "2019-04-06 16:36:33",                                
        name: "Gravity",                                                  
        logo: "spaces/4dab5e2bf8d8725b05186b9d574f8c98640526d9.png",      
        created_at: "2019-04-06 16:36:33",                                
        zip_code: "111",                                                  
        slug: "gravity",                                                  
        id: 0,                                                            
      },                                                                  
    },                                                                    
  ],   

我想要一种结构,其中我有一个成对键值数组,其格式为:["space.id"=>"role"],如果对于给定的space.id,它们的键是重复的,它应该创建一个像这个['space.id'=>[role1,role2]]。为了解决这个问题,我在Laravel集合上使用了pluck方法。但是有两个主要问题。 上一个数组的结果保存在$result变量中。在该变量上,我这样调用了pluck方法:$result->pluck('role','space.id')->all()然后我得到了:

[                             
  850 => "AUTHORS",           
  766 => "ADMINISTRATORS",    
  844 => "ADMINISTRATORS",    
  0 => "ADMINISTRATORS",      
]                             

首先,当我进行修补时,一切都很好。但是,当我将结果以json格式发送给front-end时,space.id不会出现,这就是ajax call之后的样子:

[                             
      "0" => "AUTHORS",           
      "1" => "ADMINISTRATORS",    
      "2" => "ADMINISTRATORS",    
      "3" => "ADMINISTRATORS",      
    ]   

所以,我不知道为什么原来的space.id不存在。

第二个问题:如果您仔细查看第一个数组space.id 850,它对CONTRIBUTORSAUTHORS都重复了两次,因此它不支持duplicate keys和它为相同的key 850取了最新的值。但是我想为重复的键提供类似的东西:

[                             
      850 => ["CONTRIBUTORS","AUTHORS"],//Take the 2 instead of only keep the latest value           
      766 => "ADMINISTRATORS",    
      844 => "ADMINISTRATORS",    
      0 => "ADMINISTRATORS",      
    ] 

但是我没有任何解决方案。谢谢

1 个答案:

答案 0 :(得分:0)

我的答案将取决于Laravel集合,但您可以对其进行调整以在查询级别过滤结果(因为这样会更快),但是对于少量数据集,可以用php处理它们就足够了。

请注意,JSON不允许数组键具有不连续的整数键,因此每个[id => roles[]]映射都将成为一个对象,而您的输出将是:

{
    "850": ["CONTRIBUTORS","AUTHORS"],
    "766": ["ADMINISTRATORS"],
    "844": ["ADMINISTRATORS"],
    "0": ["ADMINISTRATORS"]
}

要将查询结果转换为json输出,您必须像这样处理它:

// Query the database
$groups = $u->userGroups()->with('space')->get();

$roles = $groups->groupBy(function ($group) {
    // Group each 'UserGroup' by space.id
    return $group->space->id;
})->map(function ($group) {
    // For each group created, pluck the roles
    return $group->pluck('role');
});

dd($roles->toJson());

注意:如果您的范围是将数据作为json返回给调用者,则还可以决定使用return response()->json($roles)而不是将其转换为json并打印为字符串。 / p>