City.php 939 字节
<?php

namespace App\Models;


use App\Helper\Arr;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

/**
 * Class City
 * @package App\Models
 * @author zbj
 * @date 2023/6/20
 */
class City extends Model
{
    //设置关联表名
    protected $table = 'gl_system_city';

    public static function getTreeList(){
        $cache_key = 'city_select_tree';
        $data = Cache::get($cache_key);
        if(!$data){
            $list = self::select(['city_id', 'parent_id', 'name'])->whereIn('level', [0,1])->get()->toArray();
            $data = Arr::listToTree($list, 'city_id', 'parent_id');
            Cache::put($cache_key, $data, 24 * 3600);
        }
        return $data;
    }

    public static function source($city_id){
        $where = [
            'parent_id' => $city_id,
            'is_show' => 1,
        ];
        return self::where($where)->pluck('name', 'city_id')->toArray();
    }
}