Menu

Thursday, August 8, 2019

[Laravel - Scheduler] การใช้ Environment Variable ของระบบปฏิบัติการใน Laravel Scheduler
[Laravel - Scheduler] How to Use Environment Variable of Operating System in Laravel Scheduler

Laravel scheduler ใช้ Crontab ในการรันคำสั่งต่างๆตามเวลาที่กำหนดบน Linux

เมื่อเราต้องการใช้งาน Laravel scheduler เราเพียงเพิ่มการตั้งค่าตามด้านล่างนี้ใน Cron ของ server
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
สำหรับการใช้ Environment Variable ของระบบปฏิบัติการ เราจำเป็นต้องสร้าง shell script ขึ้นมาตัวนึง เพื่อ export ตัวแปรที่เราต้องการใช้ก่อน

Wednesday, August 7, 2019

[Laravel - Model] เรื่องที่ควรรู้เกี่ยวกับ Attributes ใน Laravel Model
[Laravel - Model] What to Know about Attributes in Laravel Model

นอกจาก Laravel Model จะมี Attributes ที่ชื่อตามชื่อ Column ของ Table ใน Database แล้ว Laravel Model ยังมีตัวแปรและ Function สำหรับการจัดการ Attributes พวกนี้อีกด้วย โดยมีรายละเอียดดังนี้

ตัวแปรสำหรับการจัดการ Attributes ใน Laravel Model ประกอบด้วย
  • $fillable - เป็นตัวแปรที่กำหนดว่า Attributes ใดสามารถแก้ไขค่าได้บ้าง
  • $cast - เป็นตัวแปรที่กำหนดว่า Attributes ใดต้องเปลี่ยนประเภทตัวแปรบ้าง ซึ่งประเภทตัวแปร ประกอบด้วย integer, real, float, double, decimal:<digits>, string, boolean, object, array, collection, date, datetime และ timestamp
  • $dates - เป็นตัวแปรที่กำหนดว่า Attributes ใดต้องถูกเปลี่ยนประเภทเป็น datetime บ้าง ซึ่ง attribute ดังกล่าวจะถูก cast เป็น Carbon instance
  • $hidden - เป็นตัวแปรที่กำหนดว่า Attributes ใดต้องถูกตัดออก เมื่อมีการแปลง Model ไปเป็น JSON
  • $appends - เป็นตัวแปรที่กำหนดว่า Attributes ใดต้องถูกเพิ่มเข้าไป เมื่อมีการแปลง Model ไปเป็น JSON

Monday, August 5, 2019

[Laravel - Model] ข้อควรระวังของการใช้ฟังก์ชั่น where และ orWhere ของ Laravel Model
[Laravel - Model] Warning: Using where and orWhere functions of Laravel Model

สมมติว่า
  • OrganizationChildren เป็น Laravel model ที่ผูกกับตาราง organization_children ในฐานข้อมูล
  • OrganizationChildren ประกอบด้วยฟิลด์ parent_organization_id และ child_organization_id 
  • $org1->id และ $org2->id เป็นค่า id ของ Organization โดยมีค่าเป็น 1 และ 2 ตามลำดับ

ถ้าเราต้องการ query ข้อมูล โดยใช้ SQL ดังนี้
select * 
from `organization_children` 
where (`parent_organization_id` = 1 and `child_organization_id` = 2) or 
(`parent_organization_id` = 2 and `child_organization_id` = 1)

[Laravel - Package] วิธีการเปลี่ยนข้อมูลที่ใช้สำหรับ Subject Claim ของ JWT ใน Laravel Model
[Laravel - Package] How to Change Data for Subject Claim of JWT in Laravel Model

JSON Web Token (JWT) ถูกใช้ใน stateless web application

jwt-auth เป็น JWT package ที่นิยมใช้สำหรับ Laravel framework

package นี้จะใช้ ID ของผู้ใช้งานเป็น sub claim (subject claim) โดยอัตโนมัติ

เนื่องจาก payload ของ JWT สามารถ decode ที่ใดก็ได้ โดยไม่จำเป็นต้องใช้ key ใดๆ การใช้ ID ของผู้ใช้งาน จึงทำให้เกิดปัญหาทางด้านความปลอดภัย

คุณสามารถเปลี่ยนฟิลด์ ID ของผู้ใช้งานเป็นฟิลด์อื่นด้วยวิธีตามด้านล่างนี้ เพื่อหลีกเลี่ยงปัญาด้านความปลอดภัย

Saturday, August 3, 2019

[PHP] วิธีการพิมพ์ข้อความออกทาง STDOUT หรือ STDERR หรือ SYSLOG
[PHP] How to Print Out Message to STDOUT or STDERR or SYSLOG

ในบางครั้งคุณมีความจำเป็นต้องพิมพ์ข้อความออกทาง system log หรือ I/O streams อื่นๆ เช่น standard output, standard error

สมมติว่า $str เป็นข้อความที่คุณต้องการ print

คุณสามารถใช้ method ข้างล่างสำหรับการพิมพ์ข้อความออก system log
syslog(LOG_INFO, $str);
อ่านข้อมูลเพิ่มเติมที่ https://www.php.net/manual/en/function.syslog.php

คุณสามารถใช้ method ข้างล่างสำหรับการพิมพ์ข้อความออก I/O streams อื่นๆ
$out = fopen('php://stdout', 'w'); //output handler
fputs($out, $str); //writing output operation
fclose($out); //closing handler
โดยที่ php://stdout สามารถแทนด้วย php://stderr หรือ php://output
อ่านข้อมูลเพิ่มเติมที่ https://www.php.net/manual/en/wrappers.php.php