Downloads
Stars
Version
思路:通过身份证前6位来处理省市区的级联关系
composer require lvqingan/laravel-cn-region:dev-master
database/migration
中的表结构,增加保存区域的字段(默认使用region
)$table->char('region', 6);
$table->index(['region']);
Eloquent
的模型中添加 HasRegion
trait默认使用
region
作为字段名,如果数据表使用的是其他字段来保存区域值,则需要定义属性$regionFieldName
class User extends Model
{
use HasRegion;
}
class User extends Model
{
use HasRegion;
protected $regionFieldName = 'shengshiqu';
}
省份
$provinces = (new \Lvqingan\Region\Loader('province'))->load();
{
"110000":"北京市",
"820000":"澳门特别行政区"
}
城市
$cities = (new \Lvqingan\Region\Loader('city', '340000'))->load();
{
"340100":"合肥市",
"341800":"宣城市"
}
区县
$districts = (new \Lvqingan\Region\Loader('340100', '340100'))->load();
{
"340103":"庐阳区",
"340111":"包河区"
}
实例化的模型对象可以调用下面的属性(假设区域值为340104)
属性名称 | 说明 | 返回值 |
---|---|---|
province_code | 省份编码 | 340000 |
city_code | 城市编码 | 340100 |
district_code | 区县编码 | 340104 |
province_name | 省份名称 | 安徽省 |
city_name | 城市名称 | 合肥市 |
district_name | 区县名称 | 蜀山区 |
full_city_name | 完整城市名称 | 安徽省合肥市 |
full_district_name | 完整区县名称 | 安徽省合肥市蜀山区 |
按区域编码直接查询区域等于该值的数据
User::whereRegion('340104')->get()
按区域编码直接查询区域包含在该范围内的数据
User::whereInRegion('340000')->get()