Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Laravel Thaiaddress Package
Atthaphon Urairat
Atthaphon Urairat

Posted on

     

Laravel Thaiaddress Package

พอดีมีโอกาสได้ไปรู้จักกับ repoฐานข้อมูล จังหวัด อำเภอ ตำบล ละติจูด ลองจิจูด
ใน GitHub เข้า เลยเอามาใช้กับโปรเจ็คทดลองที่กำลังสร้างด้วย laravel ครับ

ทั้งนี้ทั้งนั้น repo ของคุณAaron Amm มีประโยชน์มากๆ ขอขอบคุณไว้ ณ ที่นี้ด้วยครับ

เมื่อได้มีโอกาสใช้งานกับ laravel แล้วก็เลยคิดว่าน่าจะมีประโยชน์ในโปรเจ็คต่อไปในอนาคตด้วย
เลยเอามาทำเป็น laravel package เผื่อไว้ใช้งานในโปรเจ็ตต่อไป
ถ้าเพื่อนๆสนใจก็สามารถเอาไปติดตั้งใช้งานหรือเอาไปต่อยอดกันได้เลยครับ

Repositories

laravel-thai-address แพคเกจ คืออะไร

แพคเกจ laravel-thai-address คือการนำเอา ฐานข้อมูล จังหวัด อำเภอ ตำบล ที่ได้กล่าวไปแล้วข้างต้น
มาสร้าง เป็น migration สำหรับ laravel framwork โดย จะแบ่งตัว migrations ออกเป็น 3 ไฟล์
เพื่อสร้างตารางในดาต้าเบสแยกกัน

จากนั้นก็ทำการ seeds ข้อมูล ที่อยู่ในรูปแบบของ csv ไฟล์ที่มีอยู่ในแพคเกจเข้าไปยังตารางแต่ละตารางให้ถูกต้อง
หลังจากที่เราได้ทำการ migrate ตารางใหม่เสร็จเป็นที่เรียบร้อย

ติดตั้ง

เพิ่มแพคเกจเข้าไปยังโปรเจ็คของเรา

$composer require uatthaphon/laravel-thai-address
Enter fullscreen modeExit fullscreen mode

ถ้าเราใช้ laravel เวอร์ชั่นที่ตำกว่า 5.5 เราต้องทำการเพิ่ม service provide เข้าไปที่config/app.php ด้วยตนเอง

<?php'providers'=>[...Uatthaphon\ThaiAddress\ThaiAddressServiceProvider::class,],
Enter fullscreen modeExit fullscreen mode

แต่สำหรับ laravel version 5.5 ขึ้นไปผมได้เพิ่มauto discoverable เข้าไปในตัวแพคเกจเรียบร้อยแล้วทำให้เราไม่ต้องทำการเพิ่มบรรทัดเข้าไปยังconfig/app.php

จากนั้นทำการรันคำสั่ง verder publish สำหรับแท็ก 3 แท็ก

  1. $ php artisan vendor:publish --tag=migrations
  2. $ php artisan vendor:publish --tag=csv
  3. $ php artisan vendor:publish --tag=seeds

หรือถ้าต้องการ publish แท็กทั้งหมดในคราวเดียวก็สามารถทำได้

$php artisan vendor:publish
Enter fullscreen modeExit fullscreen mode

ขั้นตอนต่อไป เราจะมาทำการสร้างตารางหลังจากที่เราทำการ publish ทุกอย่างเป็นที่เรียบร้อยแล้ว

$php artisan migrate
Enter fullscreen modeExit fullscreen mode

จากนั้นก็ทำการเพิ่มข้อมูลเข้าไปยังตารางของเราด้วย seeder โดยที่ seeder
จะนำเอาไฟล์ csv ที่ตอนนี้อยู่ในโปรเจ็คของเราเป็นที่เรียบร้อยแล้ว หลังจากเราทำการ publish มันเข้ามา
และเอาข้อมูลที่ได้ ไปเพิ่มเข้าไปยังตารางแต่ละตารางอย่างถูกต้อง

$php artisan db:seed--class=ThailandAddressSeeder
Enter fullscreen modeExit fullscreen mode

การใช้งาน

ตอนนี้เราได้ดาต้าเบสที่มีข้อมูล จังหวัด อำเภอ ตำบล รหัสไปรษณีย์ ละติจูด ลองจิจูด มาแล้ว
เราสามารถนำมาประยุกต์ใช้กับโปรเจ็คของเราได้ตามใจชอบเลยครับ

ตัวช่วย

ในแพคเกจของเรามี Models relationship ติดมาให้ใช้ด้วยครับ

ถ้าหากใครต้องการใช้งานตัว Models ไฟล์ที่ผูก relationship ไว้ก็สามารถเพิ่ม psr-4 เข้าไปใน
composer.json เพื่อเรียกใช้งานในโปรเจ็คได้เลยครับ

ตอนนี้ได้ทำการอัพเดตตัว package แล้วจึงไม่จำเป็นต้องเพิ่ม psr-4 ใน composer.json
ของตัวโปรเจ็คที่เรียกใช้งานครับ ดังนั้นเราสามารถเรียกใช้ตรงๆได้เลยตามตัวอย่างด้านล่างครับ

จากนั้นก็เอาไปใช้ได้เลยครับ

<?phpuseUatthaphon\ThaiAddress\Models\ThailandProvince;useUatthaphon\ThaiAddress\Models\ThailandDistrict;useUatthaphon\ThaiAddress\Models\ThailandSubdistrict;.../** * Available Relationships */// list all districts under the provinceapp(ThailandProvince::class)->find(1)->districts()->get();// get province of the districtapp(ThailandDistrict::class)->find(1)->province;// list all sub districtes under the districtapp(ThailandDistrict::class)->find(1)->subdistricts()->get();// get district of the subdistrictapp(ThailandSubdistrict::class)->find(1)->district;// get province of the subdistrictapp(ThailandSubdistrict::class)->find(1)->province;
Enter fullscreen modeExit fullscreen mode

ถ้าใครมีข้อสงสัยก็สามารถคอมเม้นทิ้งไว้ได้นะครับ
หรือถ้ามี bug หรือ issue อะไรก็โพสไว้ที่ github ได้เลยครับ

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ballcs profile image
Wichukorn Dandecha
  • Location
    Thailand
  • Joined

เหมือนจะไปใช้กับ laravel 8 แล้วจะมี error ครับ

CollapseExpand
 
uatthaphon profile image
Atthaphon Urairat
  • Location
    Roi Et, Thailand
  • Joined

ล่าสุด มีการทดสอบถึงแค่ laravel version 5.5 ครับ.

กด watch repo ไว้ก่อนก็ได้ครับ หรือเปิด issue ไว้ใส่รายละเอียดไว้ก็ได้ครับ ช่วงที่มีเวลาเดี๋ยวไปดูให้ครับ

หรือต้องการช่วยอัพเดตก็สามารถ PR มาได้ครับ

CollapseExpand
 
ballcs profile image
Wichukorn Dandecha
  • Location
    Thailand
  • Joined

ขอเป็นผู้ใช้ก่อนดีกว่าครับ พอดีผมก็ไม่เก่งตรงนี้ด้วยครับ ยังไงก็ขอบคุณครับ

Thread Thread
 
uatthaphon profile image
Atthaphon Urairat
  • Location
    Roi Et, Thailand
  • Joined

ถ้าอย่างนั้นเปิด issue ใส่รายละเอียดตามด้านล่างไว้ที่github issue ได้เลยนะครับ เดี๋ยวผมลองไปเช็คดู

  • Laravel Version
  • PHP Version
  • DB Version (ใช้ mysql ใช่ไหมครับ)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Roi Et, Thailand
  • Joined

More fromAtthaphon Urairat

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp