I have this code:
Setting::where('section', $section)->select('key', 'value')->pluck('value')->toArray();
it produces this:
0 => "true"
1 => "native"
2 => "true"
3 => "true"
I need this
enable => "true"
type => "native"
must_be_registered => "true"
allow_nested => "true"
how do I get that? I tried pluck('key', 'value') but that did not return all values from DB for some reason some were missing, this is what came out:
array:3 [▼
"true" => "allow_nested"
"native" => "type"
]
it came out in value => key order and was missing keys and values
How can I get key => value?
答案 0 :(得分:3)
You almost got it, the reason for not getting all the values is that you interchanged your key value pair in the pluck function. And also bear in mind that array keys must be unique so php will do some truncation there.
Try this.
Setting::where('section', $section)->select('value', 'key')->pluck('value','key')->toArray();
Its seem weird but the value must come first the key after
答案 1 :(得分:0)
You are just Plucking the Value! That's why it is returning an array containing the values only!
Yes, Now you'll get Array of Arrays containing your Information.
To Solve that, Create an Empty Array and run foreach
loop Just like this.
$processed_array = array();
foreach($entries as $entry){
$processed_arrray[$entry->key] = $entry->value;
}
I think it's the best way to do it. You can create new function in your Model.php
and whenever you want the Data in key -> value
format in single array, You can just run Model::yourCustomFunction($parameters);
.
Hope this helps! Let me know if it's your solution.