对于城市数据可以参考:Laravel 多级联动
12
# 安装依赖$ composer require "overtrue/pinyin:~4.0"
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
<?phpnamespace App\Http\Controllers;use Overtrue\Pinyin\Pinyin;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;use App\Http\Controllers\Controller;class AreaController extends Controller{ /** * 地区列表 - 拼音排序 * * @return \Illuminate\Http\Response */ public function list() { $list_tmp = DB::table('area') ->select('code', 'name') ->orderBy(DB::raw('convert(name using gbk)')) ->get(); $pinyin = new Pinyin(); $list = []; $list_tmp->map(function($item) use(&$list, $pinyin) { // 去除城市名最后面的市字 // if(substr($item->name, -3) === '市') { // $item->name = substr($item->name, 0, -3); // } // 获取城市名第一个字 $name_first = substr($item->name, 0, 3); // 获取拼音 $name_pinyin = $pinyin->convert($name_first)[0]; // 获取第一个字母并且转换为大写 $name_pinyin = strtoupper(substr($name_pinyin, 0, 1)); if(!isset($list[$name_pinyin])) { $list[$name_pinyin] = []; } $list[$name_pinyin][] = (array) $item; }); return response() ->json([ 'code' => 1, 'data' => $list, ]); }}
123456789101112131415161718192021
{ "code": 1, "data": { "G": [ { "code": 5108, "name": "广元市" }, { "code": 4401, "name": "广州市" } ], "S": [ { "code": 4403, "name": "深圳市" } ] }}
hongfs.ʕ◔ϖ◔ʔ
2023 新时代的广州传统美德我在绿灯的人行道上礼让电动车
文章
181
分类
13