mark-villudo/countries-states-cities

此包的最新版本(dev-master)没有提供许可证信息。

Laravel 包,用于国家、州/省和城市

dev-master 2021-01-25 05:21 UTC

This package is auto-updated.

Last update: 2024-09-05 03:20:24 UTC


README

Laravel 包,主要用于表单下拉列表中的地址。

安装

  1. 使用 composer 安装此包。
composer require mark-villudo/countries-states-cities
  1. 现在将服务提供者在 config/app.php 文件中添加
'providers' => [
    // ...
    MarkVilludo\CountryStateCities\ServiceProvider::class,
];
  1. 发布 Seeder 和迁移文件

您可以使用以下命令发布迁移:


php artisan vendor:publish --provider="MarkVilludo\CountryStateCities\ServiceProvider" --tag="migrations"

然后,运行:


php artisan migrate

您可以使用以下命令发布 Seeder:


php artisan vendor:publish --provider="MarkVilludo\CountryStateCities\ServiceProvider" --tag="seeder"

在 DatabaseSeeder.php 中声明已发布的 Seeder

public function run()
{
  $this->call(CountryTableSeeder::class);
  $this->call(ProvincesTableSeeder::class);
  $this->call(CitiesTableSeeder::class);
}

然后,运行:


php artisan db:seed

您还可以发布其默认控制器


php artisan vendor:publish --provider="MarkVilludo\CountryStateCities\ServiceProvider" --tag="controllers"

  1. 为模型创建资源文件。对于国家、省份和城市。
php artisan make:resource CountryResource
php artisan make:resource ProvinceResource
php artisan make:resource CityResource
php artisan make:model Models/Country
php artisan make:model Models/Province
php artisan make:model Models/City
  1. 在 routes/api.php 中添加此路由

Route::prefix('v1')->group(function () {
  //Country, Province/State and City
  Route::prefix('countries')->group(function () {
    Route::get('/','Api\CountryController@index');
    Route::get('/{id}/provinces','Api\ProvinceController@index');
  });
  Route::prefix('provinces')->group(function () {
    Route::get('/{id}/cities','Api\CityController@index');
  });
});

用法

//启动应用程序。 php artisan serve

//通过 routes/api.php 中添加的代码访问 URL

http://127.0.0.1:8000/api/v1/countries
http://127.0.0.1:8000/api/v1/countries/169/provinces
http://127.0.0.1:8000/api/v1/provinces/46/cities

许可证

MIT 许可证(MIT)。请参阅许可证文件获取更多信息。