我想缓存查询并将结果作为关联数组。 当我这样做而没有这样的缓存时,它没关系:
class ItemRepositoryEloquent {
public function getItems() {
$items= Item::lists('word','key');
return $items->toArray();
}
}
但是当我尝试像这样缓存我的查询时:
class ItemRepositoryEloquent {
public function getItems() {
$items = Cache::remember('items', 1440, function(){
return Item::lists('word','key');
});
return $items->toArray();
}
}
它会返回如下结果:
[
0 => array('key' => 'word')
1 => array('key' => 'word')
2 => array('key' => 'word')
3 => array('key' => 'word')
4 => array('key' => 'word')
5 => array('key' => 'word')
]
但我希望缓存后结果如下:
[
'key' => 'word',
'key' => 'word',
'key' => 'word',
'key' => 'word',
'key' => 'word'
]
提前致谢。
答案 0 :(得分:1)
你应该能够通过使用collapse
方法得到你想要的结果,不知道什么记得默认返回,但这是一个使用原始数组的例子
$collection = collect([
0 => array('key1' => 'word'),
1 => array('key2' => 'word'),
2 => array('key3' => 'word'),
3 => array('key4' => 'word'),
4 => array('key5' => 'word'),
5 => array('key6' => 'word'),
]);
然后你可以做
$collection->collapse()->toArray();
会给你
[
"key1" => "word",
"key2" => "word",
"key3" => "word",
"key4" => "word",
"key5" => "word",
"key6" => "word",
]