我有以下收藏:
$configuration = DB::table('configuration')->get();
Illuminate\Support\Collection {#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];
});我认为这是失败的,因为上面的语句仍然返回一个数组:
Illuminate\Support\Collection {#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"
]
}有人有什么想法吗?我觉得我和它越来越近了,但是收藏品让我有点困惑。
我想我本质上是在追求这样的东西:
Illuminate\Support\Collection {#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"
}发布于 2022-06-24 07:54:43
您可以使用Arr助手来映射键值:
$configuration = DB::table('configuration')->get();
$configuration = Arr::pluck($configuration, 'configuration_value','configuration_name');此外,添加以下行以导入类:
use Illuminate\Support\Arr;https://stackoverflow.com/questions/72739481
复制相似问题