laravel config:cache creates different cache file between first run and afterwards - php

I just run into curious behavior of laravel config:cache.
When I create a .env file with
APP_ENV=production
THIS_IS_TEST_1=1
THIS_IS_TEST_3=1
(not important lines are snipped, the same goes for the others.)
and I create .env.production like
APP_ENV=production
THIS_IS_TEST_1=2
THIS_IS_TEST_2=1
then I add lines to read .env value in config/app.php
'this_is_test_1' => env('THIS_IS_TEST_1', -1),
'this_is_test_2' => env('THIS_IS_TEST_2', -1),
'this_is_test_3' => env('THIS_IS_TEST_3', -1),
After that, I've typed command ./artisan config:cache. It generates cache file as bootstrap/cache/config.php with values below.
'this_is_test_1' => '2',
'this_is_test_2' => '1',
'this_is_test_3' => '1',
So, at the first run of ./artisan config:cache generates cache file with both .env and .env.production. And also values in .env overwritten by values in .env.production.
And then, this is most curious part, I've run ./artisan config:cache following the first run generates cache file with values below.
'this_is_test_1' => '1',
'this_is_test_2' => -1,
'this_is_test_3' => '1',
This cache looks like based on .env and not on .env.production. After second run, ./artisan config:cache generates cache only with .env.
When I run config:clear and clear the cache file, then ./artisan config:cache generates config cache as same as first attempt.
'this_is_test_1' => '2',
'this_is_test_2' => '1',
'this_is_test_3' => '1',
Is this a normal and expected behavior of laravel?
I've placed entire example on github.
https://github.com/kent013/laravel-config-example
This example project is generated with composer create-project laravel/laravel example-app and just changed few lines to regenerate the behavior.
Any helps are appreciated. Thanks.

So you saying it is ignoring .env.production in second run? It could be because the both files have same APP_ENV=production. May be you should try changing APP_ENV in the .env file.

Related

Laravel cannot find public dir

I worked on a team and then clone the laravel/php codes from our repository. When I serve the laravel on localhost, it cannot find all the file inside the public directory and throw an error on the terminal:
[404]: GET /public/css/style.css - No such file or directory
Many answers to similar issues advise me to change the codes in the blade.php file. The problem is that I can't edit the blade.php because it works fine on other team members even though we have the same ubuntu, PHP, and laravel version and .env file.
What I have tried:
Run composer install and composer update
Try different browsers such as firefox and chromium-based browsers
Run php artisan storage:link
Restart apache2
Run npm install and npm run dev
I think there is some package or something missing.
List of paths I get from dd($__data):
"path" => "/project-directory/project-name/app"
"path.base" => "/project-directory/project-name"
"path.lang" => "/project-directory/project-name/resources/lang"
"path.config" => "/project-directory/project-name/config"
"path.public" => "/project-directory/project-name/public"
"path.storage" => "/project-directory/project-name/storage"
"path.database" => "/project-directory/project-name/database"
"path.resources" => "/project-directory/project-name/resources"
"path.bootstrap" => "/project-directory/project-name/bootstrap"
Have you tried?
php artisan storage:link
You need first run
php artisan storage:link
For more details check https://laravel.com/docs/8.x/filesystem
Then you must be check your .env values.
Is correctly APP_URL=
Also in your exception is strange
/public/.../...
Reason is wrong configuration.
First check your .env values like
APP_URL=...
FILESYSTEM_DRIVER=public
then check config/filesystems.php

Laravel change timezone

After changing app/config/app.php FROM: 'timezone' => 'UTC', which is the default TO: 'timezone' => 'Europe/Vilnius', and still getting wrong date output. Also I changed php.ini file
[Date]
date.timezone=Europe/Vilnius
and restarted apache server, still getting wrong date output.
Any idea how to solve this?
You can set your Application Time Zone by configuring app.php file in config folder. To change time zone
'timezone' => 'Europe/Vilnius'
And clear the config cache execute this command :
php artisan optimize:clear
You do not have the privileges to execute that command, you can delete the cached config file.
It is located at bootstrap/cache/config.php.
In order to reflect your timezone related changes of your config file then kindly ensure that you have cleared your cache. You can use the following command set for your reference:
php artisan cache:clear
php artisan config:cache
php artisan config:cache
Try to clear the config cache:
php artisan optimize:clear

Laravel database still use old name after .env changed

Edit:
php artisan config:cache work nice, but now I got other problem.
the URL giving me Error 500
I upload a project to a new subdomain area after I changed .env file
when I open URL I still got an error with the old database and user
I tried to check online with the .env file but - I don't know where he stored this database, I tried to see where is this name with ctrl+f but - nothing found
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=faceterc_hot
DB_USERNAME=faceterc_hot
DB_PASSWORD=testpro
I expect to get the same error maybe but not with the old database name.
and this gives me a indicate that maybe the file or something not changed or he using the other details from I don't know where
Good, practice for, If you change in a .env file first restart the server using below command.
php artisan serve
then after run below more command for clear old cache and config.
composer dump-autoload
php artisan config:cache
php artisan config:clear
You can simply do a
php artisan config:cache
In case you are on a shared hosting, you can change the values in config/database.php to only use the set values in your .env. Setting it like this will make sure, it only use the .env values.
mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
php artisan config:clear Solve the problem with DATABASE error
just ran :
php artisan config:clear
with ssh at the main folder.
Just execute
php artisan config:cache
as the laravel cache's the environment files rather than accessing the data from it on every request.
It's always a good practice to run these couple of commands when you change any environment related variable:
php artisan config:clear
php artisan cache:clear
Execute these commands:
php artisan config:cache
php artisan route:cache
php artisan optimize
That will clear your env cache and it should do.

Laravel 5.6 Application In Production

When am trying to migrate anything , then every-time it's asking me about
Do you want to run ".env" instead? (yes/no) [no]:
I added .env file in my project. also i tried to solve this problem with this below changes :
config/app.php
'.env' => env('APP_ENV', 'development'),
'debug' => env('APP_DEBUG', true),
.env file below :
APP_ENV=local
APP_DEGUG = true
i also tried to fix this problem with
php artisan config:clear
after this
php artisan key:generate
Nothing changes when i checked this current mode by :
php artisan env
It's showing me
Current application environment: production
how to solve this ?
I had this problem too, and these steps helped me:
clear config
php artisan config:clear
after this php artisan key:generate
now cmd say me its local
php artisan env
There's a typo:
Not:
'.env' => env('APP_ENV', 'development'),
But:
'env' => env('APP_ENV', 'development'),
without dot before env

env values from Linux not loaded by Laravel config app

Some time ago I asked about setting env settings and how to properly use them. I was quickly pointed to a comparable question and I found out that indeed it was bad practice to use env('KEY') throughout your code.
So now I am in the process of migrating my env settings to config/app.php.
However, if I play with Tinker, the env variables from Linux are not loaded by Laravel. For instance, if I place:
'test' => 'testing123',
within the config/app.php
and do a
sudo php artisan config:cache
and employ Tinker
config('app.test');
=> "testing123"
So that seems to work. However, if I place the following
'test' => env('DB_PORT'),
and do a
sudo php artisan config:cache
and test this with tinker:
config('app.test');
=> null
But when I am in the console and use:
env|grep DB_PORT
I see the right value for the DB_PORT key. I am supplying these in AWS frontend, these properties are then passed in the application as environment properties.
Anyone any idea why these are not imported/loaded correctly?
php artisan config:clear
Or you can just manually delete bootstrap/config.php, which is what artisan does after all.
See: vendor\laravel\framework\src\Illuminate\Foundation\Console\ConfigClearCommand.php

Categories