Laravel(php) —解決 env()取出的值為null

hosomikai
1 min readFeb 19, 2020

前陣子在部署Laravel專案時,遇到了一個問題。
設定完Laravel的專案的時候,執行時卻報了錯誤,分析後估計是執行的service讀取的 config 中為調用 env() 取出的環境變數為空值惹的禍。

畢竟寫程式的時候一個粗心就會debug耗半天,一開始想的可能性是,環境變數的參數名稱大小寫或手殘type wrong,確認了一下,並用 tinker 測試了一下,發現我是沒有手殘的。

後來才想到既然沒有手殘,卻沒有讀到值,那就只有可能是 Laravel快取的問題了。

執行清除快取的指令:

$ php artisan config:clear

tinker 下測試一下:

$ php artisan tinker>>> env('DB_DATABASE')

果然解決問題了!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response