将集合转换为键值集合



我有以下集合:

$configuration = DB::table('configuration')->get();
IlluminateSupportCollection {#562 ▼
#items: array:7 [▼
0 => {#1447 ▼
+"configuration_name": "assign_device_email_addresses"
+"configuration_value": "mobiles@example.com|accountspayable@example.com"
}
1 => {#1357 ▼
+"configuration_name": "helpdesk_platform_url"
+"configuration_value": "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
}
2 => {#1446 ▼
+"configuration_name": "mail_encryption"
+"configuration_value": "tls"
}
3 => {#563 ▼
+"configuration_name": "mail_host"
+"configuration_value": "exampleserver.example.com"
}
4 => {#1292 ▼
+"configuration_name": "mail_password"
+"configuration_value": "encrypted_password"
}
5 => {#1291 ▼
+"configuration_name": "mail_port"
+"configuration_value": "465"
}
6 => {#885 ▼
+"configuration_name": "mail_username"
+"configuration_value": "mobiles"
}
]
}

在这个结构中,我只能通过以下语句访问每个项:

$configuration[0]->configuration_name
$configuration[0]->configuration_value
$configuration[1]->configuration_name
$configuration[1]->configuration_value
etc.

我希望能够通过:

访问它
$configuration->assign_device_email returning "mobiles@example.com|accountspayable@example.com".
$configuration->mail_host returning "exampleserver.example.com".
etc.

我怎样才能转换这个集合,以便我可以通过它的configuration_name访问每个属性值?

我已经尝试了下面的方法,让我更接近,但我仍然无法通过以下语句访问它:$configuration->mail_port等

$configuration2 = DB::table('configuration')->get()
->mapWithKeys(function($configuration2){
return [$configuration2->configuration_name => $configuration2->configuration_value]; 
});

我认为这失败了,因为上面的语句仍然返回一个数组:

IlluminateSupportCollection {#1500 ▼
#items: array:7 [▼
"assign_device_email_addresses" => "mobiles@example.com|accountspayable@example.com"
"helpdesk_platform_url" => "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
"mail_encryption" => "tls"
"mail_host" => "exampleserver.example.com"
"mail_password" => "encrypted_password"
"mail_port" => "465"
"mail_username" => "mobiles"
]
}

有人有什么想法吗?我觉得我离它越来越近了,但是集合让我有点困惑。

我想我基本上是这样的:

IlluminateSupportCollection {#1507 ▼
+"assign_device_email_addresses" : "mobiles@example.com"
+"helpdesk_platform_url" : "https://cloudesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
+"mail_encryption" : "tls"
+"mail_host" : "emailhost@example.com"
+"mail_password" : "encrypted_password"
+"mail_port" : "465"
+"mail_username" : "mobiles"
}

您可以在Arrhelper中映射键值:

$configuration = DB::table('configuration')->get();
$configuration =  Arr::pluck($configuration, 'configuration_value','configuration_name');

use IlluminateSupportArr;

最新更新