Skip to content

Commit

Permalink
update models, changelog, readme and insall command
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangphidev committed Nov 22, 2020
1 parent 11feb21 commit 031c330
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 14 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## v1.1 - 2020-11-22

### Fixed

- Add Models
- Add install command
- Update and fix project

## v1.0 - 2020-11-22

### Added

- Everything, initial release
127 changes: 120 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
## VietNam Maps
## Vietnam Maps

Database of administrative units of Vietnam
Database of Vietnam's area.

Data are taken directly from the General Statistics Office of Vietnam.

Make sure it is always the most current and accurate.

[![Latest Version on Packagist](https://img.shields.io/packagist/v/hoangphi/vietnam-maps.svg?style=flat-square)](https://packagist.org/packages/hoangphi/vietnam-maps)
[![Total Downloads](https://img.shields.io/packagist/dt/hoangphi/vietnam-maps.svg?style=flat-square)](https://packagist.org/packages/hoangphi/vietnam-maps)

## Install

```shell
composer require hoangphi/vietnam-maps
```

## Extracting

### Method 1:

Extract directly via command:

```shell
php artisan vietnam-map:install
```

### Method 2:

#### Copy file config và migration

```shell
Expand Down Expand Up @@ -50,21 +63,121 @@ Open file `config/vietnam-maps.php` and config:
Open the following migration files and customize if you need:

```shell
database/migrations/2020_01_01_000000_create_vietnam_maps_table.php
database/migrations/{datetime}_create_vietnam_maps_table.php
```

## Run migration
#### Run migration

```shell
php artisan migrate
```

## Download và import into database
#### Download và import into database

```shell
php artisan vietnam-map:download
```

## Usage with Models

1. Get all provinces, districts, wards

```php
use HoangPhi\VietnamMap\Models\Province;
use HoangPhi\VietnamMap\Models\District;
use HoangPhi\VietnamMap\Models\Ward;

class DevController extends Controller
{
...
public function dev()
{
$provinces = Province::all();
$districts = District::all();
$wards = Ward::all();
...
}
}
```

2. Get data using relationship

```php
use HoangPhi\VietnamMap\Models\Province;

class DevController extends Controller
{
...
public function dev()
{
$province = Province::first();
$districts = $province->districts;
...
}
}
```
3. Relation in Province.php

```php
class Province extends Model
{
...
public function districts()
{
return $this->hasMany(District::class);
}
}
```

4. Relation in District.php

```php
class District extends Model
{
...
public function province()
{
return $this->belongsTo(Province::class, config('vietnam-maps.columns.province_id'), 'id');
}

public function wards()
{
return $this->hasMany(Ward::class);
}
}
```

5. Relation in Ward.php

```php
class Ward extends Model
{
...
public function district()
{
return $this->belongsTo(District::class, config('vietnam-maps.columns.district_id'), 'id');
}
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Security

If you discover any security-related issues, please email [hoangphidev@gmail.com](mailto:hoangphidev@gmail.com) instead of using the issue tracker.

## Credits

- [Phi Hoang](https://github.com/hoangphidev)
- [All Contributors](../../contributors)

## References

1. [General Statistics Office of Vietnam](https://www.gso.gov.vn/dmhc2015)
2. [Vietnam Zone](https://github.com/kjmtrue/vietnam-zone)

## License

The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
26 changes: 23 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
{
"name": "hoangphi/vietnam-maps",
"description": "Package about Vietnam area",
"description": "Vietnam's area database.",
"type": "library",
"license": "MIT",
"keywords": [
"hoangphidev",
"vietnam-maps",
"vietnam-zone",
"vietnam-database",
"laravel",
"laravel-package",
"laravel-package-vietnam",
"laravel-vietnam-maps",
"hanhchinhvietnam",
"hcvn"
],
"homepage": "https://github.com/hoangphidev/vietnam-maps",
"authors": [
{
"name": "Phi Hoang",
"email": "hoangphi.dev@gmail.com"
"email": "hoangphi.dev@gmail.com",
"homepage": "https://github.com/hoangphidev/vietnam-maps",
"role": "Developer"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.2|^8.0",
"illuminate/support": "^5.8|^6|^7|^8",
"illuminate/database": "^5.8|^6|^7|^8",
"illuminate/console": "^5.8|^6|^7|^8",
Expand All @@ -22,11 +38,15 @@
"HoangPhi\\VietnamMap\\": "src/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"HoangPhi\\VietnamMap\\VietnamMapServiceProvider"
]
}
}
},
"prefer-stable": true
}
31 changes: 31 additions & 0 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace HoangPhi\VietnamMap\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class InstallCommand extends Command
{
protected $signature = 'vietnam-map:install';

protected $description = 'Vietnam maps install auto import into database';

public function __construct()
{
parent::__construct();
}

public function handle()
{
$this->info('Please wait for installing...');

Artisan::call('vendor:publish', ['--provider' => 'HoangPhi\VietnamMap\VietnamMapServiceProvider']);
Artisan::call('migrate');
Artisan::call('config:cache');
Artisan::call('config:clear');
Artisan::call('vietnam-map:download');

$this->info('Completed to install Vietnam\'s area database !');
}
}
26 changes: 26 additions & 0 deletions src/Models/District.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace HoangPhi\VietnamMap\Models;

use Illuminate\Database\Eloquent\Model;

class District extends Model
{
protected $guarded = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTable(config('vietnam-maps.tables.districts'));
}

public function province()
{
return $this->belongsTo(Province::class, config('vietnam-maps.columns.province_id'), 'id');
}

public function wards()
{
return $this->hasMany(Ward::class);
}
}
21 changes: 21 additions & 0 deletions src/Models/Province.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HoangPhi\VietnamMap\Models;

use Illuminate\Database\Eloquent\Model;

class Province extends Model
{
protected $guarded = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTable(config('vietnam-maps.tables.provinces'));
}

public function districts()
{
return $this->hasMany(District::class);
}
}
21 changes: 21 additions & 0 deletions src/Models/Ward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HoangPhi\VietnamMap\Models;

use Illuminate\Database\Eloquent\Model;

class Ward extends Model
{
protected $guarded = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTable(config('vietnam-maps.tables.wards'));
}

public function district()
{
return $this->belongsTo(District::class, config('vietnam-maps.columns.district_id'), 'id');
}
}
21 changes: 17 additions & 4 deletions src/VietnamMapServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@
namespace HoangPhi\VietnamMap;

use HoangPhi\VietnamMap\Console\Commands\DownloadCommand;
use HoangPhi\VietnamMap\Console\Commands\InstallCommand;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class VietnamMapServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/vietnam-maps.php', 'vietnam-maps');
$this->mergeConfigFrom(__DIR__ . '/../config/vietnam-maps.php', 'vietnam-maps');
}

public function boot()
public function boot(Filesystem $filesystem)
{
$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
__DIR__ . '/../database/migrations/create_vietnam_maps_table.php' => $this->generateMigrationFileName($filesystem),
], 'migrations');

$this->publishes([
__DIR__.'/../config/vietnam-maps.php' => config_path('vietnam-maps.php'),
__DIR__ . '/../config/vietnam-maps.php' => config_path('vietnam-maps.php'),
], 'config');

$this->commands([
DownloadCommand::class,
InstallCommand::class,
]);
}

protected function generateMigrationFileName(Filesystem $filesystem) : string
{
$timestamp = date('Y_m_d_His');
return Collection::make($this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR)
->flatMap(function ($path) use ($filesystem) {
return $filesystem->glob($path.'*_create_vietnam_maps_table.php');
})->push($this->app->databasePath() . "/migrations/{$timestamp}_create_vietnam_maps_table.php")->first();
}
}

0 comments on commit 031c330

Please sign in to comment.