Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dilee committed May 14, 2023
1 parent 0b3b5a4 commit ddaeb2d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
build
composer.lock
coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
Expand Down
62 changes: 54 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can install the package via composer:
composer require dilee/laravel-vietnamese-administrative-units
```

You can publish and run the migrations with:
You should publish and run the migrations with:

```bash
php artisan vendor:publish --tag="laravel-vietnamese-administrative-units-migrations"
Expand All @@ -25,7 +25,7 @@ php artisan migrate
You can publish the config file with:

```bash
php artisan vendor:publish --tag="laravel-vietnamese-administrative-units-config"
php artisan vendor:publish --tag="vietnamese-administrative-units-config"
```

This is the contents of the published config file:
Expand All @@ -49,19 +49,65 @@ return [
];
```

Optionally, you can publish the views using
## Usage

![image](./docs/database/schema.excalidraw.png)

As you can see, the package provides 3 models: `Province`, `District`, and `Ward`. Each model uses soft delete. (because in Vietnam, administrative units probably change in the future)

### Import data

The first thing you need to do is import administrative units to the database. You can use the command:

```bash
php artisan vendor:publish --tag="laravel-vietnamese-administrative-units-views"
php artisan vietnamese-administrative-units:import
```

## Usage
By default, this command will use a CSV file placed in `./assets/vietnamese-administrative-units.csv`. You can change the file using by passing a path to the file as an argument:

```bash
php artisan vietnamese-administrative-units:import /home/dilee/Downloads/vietnamese-administrative-units.csv
```

If you wonder where the CSV file comes from, you can find it [here](https://www.gso.gov.vn/phuong-phap-luan-thong-ke/danh-muc/don-vi-hanh-chinh/) check on "Quận Huyện, Phường Xã" and click "Xuất Excel" to download the Excel file, next you should manually convert the Excel file to CSV file without changing any data.

### Using models

You can use the models as usual:

```php
$vietnameseAdministrativeUnits = new Dilee\VietnameseAdministrativeUnits();
echo $vietnameseAdministrativeUnits->echoPhrase('Hello, Dilee!');
use Dilee\VietnameseAdministrativeUnits\Models\Ward;

class YourModel {
public function ward()
{
return $this->belongsTo(
config('vietnamese-administrative-units.ward.model', Ward::class)
)->withTrashed();
}
}
```

As I said before, each model uses soft delete, so you should use `withTrashed()` to get a relationship regardless the model is deleted or not.

And don't forget to define a foreign key in your migration:

```php
return new class extends Migration
{
public function up()
{
Schema::table('your_table', function (Blueprint $table) {
$table->foreignId('ward_id')->constrained();
});
}
}
```

### Can I use the import command many times?

Yes, you can. The command is smart enough to check if the administrative unit is already in the database, it will skip that unit and soft delete any unit that is not in the CSV file.

## Testing

```bash
Expand All @@ -82,7 +128,7 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Dilee](https://github.com/dilee)
- [Dilee](https://github.com/dileedotdev)
- [All Contributors](../../contributors)

## License
Expand Down
Binary file added docs/database/schema.excalidraw.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ddaeb2d

Please sign in to comment.