Menu

Showing posts with label artisan. Show all posts
Showing posts with label artisan. Show all posts

Thursday, April 4, 2019

[Laravel - Exception] วิธีการแก้ไข exception หลังจากพลาดรันคำสั่ง php artisan config:cache
[Laravel - Exception] How to Fix Exception after Running php artisan config:cache Command

คำอธิบายของคำสั่ง config:cache คือ สร้าง cache file สำหรับการโหลดค่า config ที่เร็วขึ้น (คุณสามารถรันคำสั่ง php artisan ใน console เพื่อดูคำอธิบายของแต่ละคำสั่งได้)

อย่างไรก็ตาม ผลกระทบจากการรัน  php artisan config:cache อาจจะประกอบด้วย
  1. Website/APIs ของคุณจะตอบกลับด้วย error code 500 Internal Server Error เสมอ โดยไม่มี log ใดๆใน laravel.log
  2. ทุกคำสั่งที่รันด้วย php artisan จะ throw exception ตามภาพด้านล่าง
  3. ทุกคำสั่งที่รันด้วย composer จะรันได้ตามปกติ

วิธีแก้ไข exception นี้ คือ การลบ cache file ที่ถูกสร้างขึ้นมา ซึ่งสำหรับ Laravel 5.6 cache file ดังกล่าวจะอยู่ที่ bootstrap/cache/config.php

Tuesday, February 5, 2019

[Laravel - Command] วิธีการเรียกใช้ command ที่ถูกต้อง จากอีก command นึงใน Laravel
[Laravel - Command] How to Call Laravel Command from Other Commands

บางครั้งเราต้องการสร้าง command เพื่อเรียกใช้ชุด command ที่ได้สร้างไว้ก่อนหน้าแล้ว

สมมติว่า มี 2 commands คือ command A และ B แล้วเราสร้าง command C มาอีกตัว เพื่อเรียกใช้ 2 commands นี้

ถ้าหากเป็นการเรียกรัน command จาก controller ปกติ ก็จะใช้คำสั่งดังนี้
Artisan::call('A');
Artisan::call('ฺB');

แต่สำหรับการเรียกรัน command จากอีก command นึง ห้ามใช้ Artisan::call โดยเด็ดขาด เนื่องจากถ้าหากเกิด exception ขึ้น มันจะไม่ throw exception ออกมายัง command C ทำให้ command C สามารถรันต่อจนเสร็จได้ ถึงแม้จะเกิด exception ใน command A หรือ B ซึ่งเราจะไม่สามารถรู้ได้เลยว่า มันทำงานเสร็จแล้วจริงๆหรือมันเกิด exception ขึ้น ถ้าเราไม่ดูจาก error logs

ในความเป็นจริงแล้ว เราควรจะรู้ว่า command ใดเกิด exception ขึ้น และไม่มีความจำเป็นที่ต้องรัน command อื่นๆต่อไป ในกรณีที่ exception ได้เกิดขึ้นแล้ว ดังนั้นให้ใช้ $this->call แทนดังนี้
$this->call('A');
$this->call('ฺB');

อ่านเพิ่มเติม: https://laravel.com/docs/5.7/artisan#calling-commands-from-other-commands