where(compact('name', 'level', 'pid')) ->value('id') ?: static::add($name, $level, $pid); } /** * @param $name * @param int $level * @param int $pid * @return unied */ private static function add($name, $level = 0, $pid = 0) { $model = new static; $model->save(compact('name', 'level', 'pid')); Cache::rm('area'); return $model->getLastInsID(); } /** * 获取所有地区(树状结构) * @return unied */ public static function getCacheTree() { return self::regionCache()['tree']; } /** * 获取所有地区 * @return unied */ public static function getCacheAll() { return self::regionCache()['all']; } /** * 获取地区缓存 * @return unied */ private static function regionCache() { if (!Cache::get('area')) { // 所有地区 $all = $allData = self::useGlobalScope(false)->column('id, pid, name, level', 'id'); // 格式化 $tree = []; foreach ($allData as $pKey => $province) { if ($province['level'] === 1) { // 省份 $tree[$province['id']] = $province; unset($allData[$pKey]); foreach ($allData as $cKey => $city) { if ($city['level'] === 2 && $city['pid'] === $province['id']) { // 城市 $tree[$province['id']]['city'][$city['id']] = $city; unset($allData[$cKey]); foreach ($allData as $rKey => $region) { if ($region['level'] === 3 && $region['pid'] === $city['id']) { // 地区 $tree[$province['id']]['city'][$city['id']]['region'][$region['id']] = $region; unset($allData[$rKey]); } } } } } } Cache::set('area', compact('all', 'tree')); } return Cache::get('area'); } }