lvqingan / laravel-cn-region
该包最新版本(dev-master)没有提供许可证信息。
Laravel中国省市区无数据库实现方案
dev-master
2019-05-05 02:36 UTC
Requires
- php: >=7.0
- illuminate/database: 5.*
- illuminate/events: 5.*
- illuminate/support: 5.*
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-05 14:38:55 UTC
README
中国行政区划省市区(无数据表实现方案)
思路:通过身份证前6位来处理省市区的级联关系
安装
- 安装代码库
composer require lvqingan/laravel-cn-region:dev-master
- 修改
database/migration中的表结构,增加保存区域的字段(默认使用region)
$table->char('region', 6); $table->index(['region']);
- 在
Eloquent的模型中添加HasRegiontrait
默认使用
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)
数据查询
按区域编码直接查询区域等于该值的数据
User::whereRegion('340104')->get()
按区域编码直接查询区域包含在该范围内的数据
User::whereInRegion('340000')->get()