kodepandai/laravel-indonesia

为 Laravel 提供的印度尼西亚行政数据

v2.0.3 2024-04-02 07:56 UTC

README

印度尼西亚行政数据的 Laravel 包。

这是 laravolt/indonesia 的轻量级版本,**仅**提供模型、迁移、种子和简单的 API 端点。

比较

安装

使用 composer 安装

composer require kodepandai/laravel-indonesia

(可选) 发布包迁移和配置

php artisan vendor:publish --provider="KodePandai\Indonesia\IndonesiaServiceProvider"

配置

打开 config/indonesia.php 文件并按需调整。

用法

种子

此包自动加载印度尼西亚迁移,但为了填充数据库,您必须手动进行配置。

在您的 DatabaseSeeder 中调用 IndonesiaDatabaseSeeder

// file: database/seeders/DatabaseSeeder.php

use KodePandai\Indonesia\IndonesiaDatabaseSeeder;

//..
public function run(): void
{
    $this->call(IndonesiaDatabaseSeeder::class);
}
//..

模型

此包包含 4 个基本模型(ProvinceCityDistrictVillage),每个模型都与其他模型相关联。

use \KodePandai\Indonesia\Models\Province;
use \KodePandai\Indonesia\Models\City;
use \KodePandai\Indonesia\Models\District;
use \KodePandai\Indonesia\Models\Village;

$province = Province::first();
$province->cities; // get cities of the province
$province->districts; // get districts of the province
$province->villages; // get villages of the province

$city = City::first();
$city->province; // get province of the city
$city->districts; // get districts of the city
$city->villages; // get villages of the city

$district = District::first();
$district->province; // get province of the district
$district->city; // get city of the district
$district->villages; // get villages of the district

$village = Village::first();
$village->province; // get province of the village
$village->city; // get city of the village
$village->district; // get district of the village

API

此包提供 API 端点以获取行政数据。API 默认启用,要禁用它,请更改配置文件。

省份

  • 获取所有省份
$ curl "https://:8000/api/indonesia/provinces"

*注意:添加参数 as_html=true 以获取 HTML 选项格式的响应。

城市

  • 获取所有城市
$ curl "https://:8000/api/indonesia/cities"
  • 按 province_code 或 province_name 获取城市
$ curl "https://:8000/api/indonesia/cities?province_code=33"
$ curl "https://:8000/api/indonesia/cities?province_name=PAPUA"

*注意:添加参数 as_html=true 以获取 HTML 选项格式的响应。

  • 获取所有区
$ curl "https://:8000/api/indonesia/districts"
  • 按 city_code 或 city_name 获取区
$ curl "https://:8000/api/indonesia/districts?city_code=3315"
$ curl "https://:8000/api/indonesia/districts?city_name=KOTA SEMARANG"

*注意:添加参数 as_html=true 以获取 HTML 选项格式的响应。

村庄

  • 获取所有村庄
$ curl "https://:8000/api/indonesia/villages"
  • 按 district_code 或 district_name 获取村庄
$ curl "https://:8000/api/indonesia/villages?district_code=337401"
$ curl "https://:8000/api/indonesia/villages?district_name=SEMARANG TENGAH"

*注意:添加参数 as_html=true 以获取 HTML 选项格式的响应。