soap/thai-addresses

Thai provinces database for Laravel application

v3.1.1 2024-04-24 07:52 UTC

README

Latest Version on Packagist run-tests Check & fix styling Total Downloads

This package provides basic thailand provinces database including districts and subdistricts. Addressable models also provided to use with any Eloquent models.

Version Support

Support us

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Requirements

soap/thai-addresses v2.x for Laravel 9 and PHP 8.0 or 8.1. If you use Laravel 8, please see release 1.x then.

Installation

You can install the package via composer:

composer require soap/thai-addresses

You can publish the config file with:

php artisan vendor:publish --tag="thai-addresses-config"

This is the contents of the published config file:

return [
    // model definition
    "geography" => [
        "table_name" => "thai_geographies",
        "foreign_key" => "geography_id"
    ],

    "province" => [
        "table_name" => "thai_provinces",
        "foreign_key" => "province_id"
    ],
    
    "district" => [
        "table_name" => "districts",
        "foreign_key" => "district_id"
    ],

    "subdistrict" => [
        "table_name" => "subdistricts",
    ],

    "address" => [
        "table_name" => "addresses",
         "model" => \Soap\ThaiAddresses\Models\Address::class
    ]
];

You can change table name for all models in the configuration file.

Then you can publish and run the migrations with:

php artisan vendor:publish --tag="thai-addresses-migrations"
php artisan migrate

Optionally, you can install configuration and migration files using install command.

php artisan thai-addresses:install

Usage

If you want to use only province, district and subdistrict data, you can just run database seeding.

php artisan thai-addresses:db-seed

This will install all thai addresses data to the database as configure in the thai-addresses.conf file.

Manage your address

To add addresses support to your eloquent models simply use \Soap\ThaiAddresses\Traits\HasAddress trait. This package provide polymorphic addressable model. By using this feature, any model can have addresses.

In your App\Models\User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Soap\ThaiAddresses\Tests\Database\Factories\UserFactory;
use Soap\ThaiAddresses\Traits\HasAddress;

class User extends Authenticatable
{
    use HasFactory;
    use HasAddress;

    protected $guarded = [];

    protected $fillable = [
        'name',
        'email',
    ];

}

Then your user can have addresses!

// Get instance of your model
$user = new \App\Models\User::find(1);

// Create a new address
$user->addresses()->create([
    'label' => 'Default Address',
    'given_name' => 'Prasit',
    'family_name' => 'Gebsaap',
    'organization' => 'KPS Academy',
    'street' => '1/8 Watchara road',
    'subdistrict_id' => Subdistrict::where('name_th','=','กระบี่ใหญ่')->first()->id,
    'latitude' => '31.2467601',
    'longitude' => '29.9020376',
    'is_primary' => true,
    'is_billing' => true,
    'is_shipping' => true,
]);

// Create multiple new addresses
$user->addresses()->createMany([
    [...],
    [...],
    [...],
]);

// Find an existing address
$address = app('thai-addresses.address.model')->find(1);

// Update an existing address
$address->update([
    'label' => 'Default Work Address',
]);

// Delete address
$address->delete();

// Alternative way of address deletion
$user->addresses()->where('id', 123)->first()->delete();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.