'Laravel env('APP_URL') is not returning the correct value for localhost

When trying to retrieve the APP_URL from the Laravel config it returns the wrong URL for development only.

My env file has the following:

APP_URL=http://127.0.0.1:9000

However when I call env('APP_URL') it returns me:

'http://localhost'

Which will not work with my current docker set-up it has to be 127.0.0.1:9000

In my config/app.php file I have the following:

'url' => env('APP_URL')

I have tried php artisan config:cache and php artisan config:clear but I still get the same result of http://localhost

Any ideas on where it could be getting http://localhost from other than the .env or config/app.php?


Thought it would be worth noting using config('app.url') also returns http://localhost



Solution 1:[1]

I found out there is an undocumented feature when loading .env files. If you have a .env.dev file in your project folder it will load this config over everything else if your application is in dev mode. It's documented that .env.testing is used in testing which makes sense but it also does it for dev with .env.dev

The application I was working on had a .env.dev and it was this file that had the http://localhost string inside of it.

Solution 2:[2]

"localhost" is probably the default value. You error indicates that the .env file is not read at all. Make sure you're not editing .example.env, check if other env variables are accessible, check for typos, check file permissions on .env and if it's located in the root folder of the project.

Solution 3:[3]

Just in case anyone is having this issue in production, these are the steps that worked for me:

  1. Make sure that the .env file is specifying that the app is in production. APP_ENV=production
  2. Run php artisan optimize:clear

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark
Solution 2 Pavel Lint
Solution 3 aifodu