This package provides basic thailand provinces database including districts and subdistricts. Addressable models also provided to use with any Eloquent models.
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.
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.
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
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.
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();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.