[英]Is it possible to generate associative array from laravel pluck()
ModalName::pluck('id')->toArray();
i want id to be associative array with certain defined key.我希望 id 是具有某些定义键的关联数组。 such as 'my_key'=>id in a pluck
比如 'my_key'=>id in a pluck
First of all pluck() return you already an array so no need to call toArray().首先,pluck() 返回你已经是一个数组,所以不需要调用 toArray()。
Yes you can make it associative by passing another argument on pluck method.是的,您可以通过在 pluck 方法上传递另一个参数来使其具有关联性。 Like this
像这样
$plucked = $collection->pluck('name', 'product_id');
sample result样本结果
['prod-100' => 'Desk', 'prod-200' => 'Chair']
please see docs here source请在此处查看文档源
so in your case所以在你的情况下
ModalName::all()->pluck('id', 'name'); // name = field in your table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.