Laravel can't change config value - laravel

I found the problem, i'll try to use a homestead or docker.
if i restart php artisan serve the changed values are applied.
Same my post: Laravel can't change config value
I make route
Route::get('/config', function () {
return dd(config('app'));
});
And cant change my config/app.php, for example i want to change
'url' => env('APP_URL', 'test'),
'client_url' => env('APP_CLIENT_URL', 'test'),
I set
APP_CLIENT_URL=str1
APP_URL=str2
and there is something inexplicable:
My "url" => "http://127.0.0.1:8000" !!! not default 'test' or from .env, I did not set such a value.
My client_url contains the old value, after I cached the config.
This is not production, i accidentally run config:cache, but after i remove all cache from bootsrap/cache, storage/framework/cache and running cache:clear, config:clear.
After running config:cache values from the .env were put in config, but I do not want to constantly change cache in development and it is not recommended in the documentation.
How can I easily change .env in in developing?

Related

I cannot disable Laravel debug logging?

I kept running the debug logging level for some time for my application also on prod, after now 2 weeks, I wanted to disable it. So I went ahead and opend the .env variable and set APP_DEBUG=false
I did a php artisan down, php artisan cache:clear, then php artisan config:clear, php artisan config:cache and brought it back up. To be fully safe here.
I went back in the log file after a minute or so and still saw production.DEBUG appearing.
I then retried all the steps, checked the config/app.php file and made sure that nothing is wrong in there. But everything looks fine.
Then I went to my local environment and tried the same but to my surprise the application still keeps logging debug messages also on local!
I cannot get it disabled!
What I tried:
php artisan config:clear / :cache / cache:clear optimize:clear / down / up ...
I checked the app.php file, the config/logging.php but all looks fine
APP_ENV=production, APP_DEBUG=false
Then to my surprise I tried something:
I changed LOG_LEVEL=info in the .env file -> no change
I opened the config/logging.php and went to the daily section (because LOG_CHANNEL=daily) and set the 'level' entry to 'info' => IT WORKS! both on PROD and LOCAL
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'info',
'days' => 21,
],
Can someone explain to me, why this is happening?
I was under the impression that the APP_DEBUG and the LOG_LEVEL should be the key indicators for Laravel but somehow they aren't?!
Might that be related to the LARAVEL DEBUGBAR? I have it installed.
Laravel version 7, php 7.4 (local Homestead, prod apache)

Laravel after upgrade, .env variable Undefined index

I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Try using the env helper instead, env('API_DOMAIN')
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env() helper outside of config files, because when you cache your config, env() will return null.

Laravel - accessing .env variables

I tried to get the environment variable from the .env in my root with
Route::get('/test', function () {
return "value is". getenv('APP_ENV');
});
and
Route::get('/test', function () {
return "it is". env('APP_ENV');
});
It is in .env
APP_NAME=Laravel
APP_ENV=local
How can I get access to it?
With Laravel, you should avoid environmental variables outside of your configuration files.
In your config files, you can use environmental variables, example in config/app.php:
'env' => env('APP_ENV', 'production'),
Then you can access this using the config helper: config('app.env').
This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached.
Route::get('/test', function () {
return "it is".config('app.name');
});
use env('ENVKEY')
Don't forget to clear cache sometime it cause because of cache.
php artisan config:clear
php artisan cache:clear
composer dump-autoload
For more info look at the doc
just run this commands in cmd.
php artisan config:cache
then
php artisan config:clear
then
php artisan cache:clear
As per the Laravel Documentation on Environment Configuration,
All of the variables listed in this file will be loaded into the $_ENV
PHP super-global when your application receives a request. You may use
the env helper to retrieve values from these variables.
So, it is possible to access the variable as
$_ENV['envKey'];
laravel provides a global helper function for this kind of task
$val = config('app.something');
you can also set new values with the following method
config(['app.something' => 'cat']);
reference
for your particular task it would be
$val = config('app.env');
or to determine the env globally
$environment = App::environment();
i hope this helps, have a nice one!
App::environment()
try this please

Laravel keeps using hello#example.com with changed conf

In mail.php I have changed from fields but I still get mails with demo values. There is no reference to hello#example.com in project, yet I still get this in mail.
'from' => [
'address' => 'do-not-reply#mysite.net',
'name' => 'MySite.net',
],
Does anyone have an idea what might be wrong?
Clear the config cache by running the following Artisan command:
php artisan config:clear
If you are using supervisor, ensure you set the right user in the conf file, otherwise you will see the emails are being sent as hello#example.com.
In my case the user is www-data - I set it up as root before and was getting this issue.

Why Laravel uses wrong database file after setting environment?

In bootstrap/start.php I have the following:
$env = $app->detectEnvironment(function()
{
if($myenv = getenv('APPLICATION_ENV')):
return $myenv;
else:
return 'local';
endif;
});
Ok so I setup a local folder and put in a database.php file with my local connections.
Just to make sure its picking up the correct environment I put in the template: {{ App::environment(); }} which outputs local.
But when making a DB call its giving me error: Undefined index: DB1_HOST
My base (production) database.php file has:
'host' => $_SERVER["DB1_HOST"],
'database' => $_SERVER["DB1_NAME"],
'username' => $_SERVER["DB1_USER"],
'password' => $_SERVER["DB1_PASS"],
Why is it looking at the production database file?
Laravel also stores the config information in
bootstrap/cache/config.php
In some cases it's not updated which may result in wrong database information. Deleting the file should resolve the issue.
If you are trying to use artisan in your terminal and you want to set the environment variable once and for all, you can do :
export APPLICATION_ENV=local
And check you current environment using php artisan env
Production config files are loaded first and then merged with overrides from other environments. If the production file generates an error (e.g. undefined index) the config loading will bail early without loading the overrides. In the production config file, check the value is set before attempting to use it and the local config file will then load correctly.
Clean the config cache as it may affect
php artisan config:cache

Resources