Laravel pluck 指定字段为数组key
/** * 获取erp 里的单位 * @return mixed */ protected function get_units(){ $key = 'erp:units'; if(Cache::has($key)){ return Cache::get($key); } else { $units = ErpUnit::all()->pluck(null,'name')->toArray(); if($units){ Cache::forever($key, $units); } return $units; } }
效果:
array:6 [▼ "g" => array:11 [▼ "id" => 1 "pid" => 0 "type" => "weight" "name" => "g" "name_cn" => "克" "rate" => "1.000000" "user_id" => 1 "deleted_at" => null "created_at" => null "updated_at" => null "rank" => 0 ] "ml" => array:11 [▶] "ton" => array:11 [▶] "kg" => array:11 [▶] "mg" => array:11 [▶] "l" => array:11 [▶] ]