I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.
My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?
In my command I tried doing $this->call('build:project') in my actual command, but even in the second call the variables are not reloaded.
Is there a way to achieve this without making the user manually call multiple commands?
Thanks!
i had the same problem with reloaded .env variables, and i fixed it with this command line which allow you to clear the configuration :
php artisan config:clear
Hope that helped. Regards.
Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:
config(['database.default' => 'mysql']);
Try to clear cache it helped me (couldn't ssh into the server)
Is there a {app route}/bootstrap/cache/config.php file on the production server? Delete it.
This helped me
As OP I'm trying to bootstrap a Laravel project build by running console command and asking for the database credentials in the middle of the process.
This is a tricky problem and nothing I read was able to fix it : reset config, cache, Dotenv reload, etc... It seems that once the console command / operation is initialized the initial database connection is kept all over until the end.
The working solution I found is to bypass, after database modification are done, this cached state by using native shell exec command and passing the php artisan command as parameter :
passthru('php artisan migrate');
So, the order will be :
php artisan project:build (or whatever is your console command)
prompt the user for database credentials
replace values in .env file (your search & replace algorythm)
run php artisan config:cache
passthru('php artisan migrate'); ro run your migrations
shell_exec would do the same but in silent mode while passthru will return the output generated by console.
https://www.php.net/manual/en/function.passthru.php
Done successfully on Laravel 8.52
Related
I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english
The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.
My Laravel 5 has run OK until the database was configured, then found this error:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Doing some research it seems that I configured MySQL access too late, so I should restart the server in order to get the correct environment variables. Well, I'm using Dreamhost's shared server and I just can't do that.
How should I fix this issue?
Thanks
If you have run php artisan config:cache on your server, then your Laravel app could cache outdated config settings that you've put in the .env file.
Run php artisan config:clear to fix that.
I know this is old, but for local dev, this is what got things back to a production .env file:
rm bootstrap/cache/config.php
then
php artisan config:cache
php artisan config:clear
php artisan cache:clear
It's possible that your configuration variables are cached. Verify your config/app.php as well as your .env file then try
php artisan cache:clear
on the command line.
To be clear there are 4 types of caches you can clear depending upon your case.
php artisan cache:clear
You can run the above statement in your console when you wish to clear the application cache. What it does is that this statement clears all caches inside storage\framework\cache.
php artisan route:cache
This clears your route cache. So if you have added a new route or have changed a route controller or action you can use this one to reload the same.
php artisan config:cache
This will clear the caching of the env file and reload it
php artisan view:clear
This will clear the compiled view files of your application.
For Shared Hosting
Most of the shared hosting providers don't provide SSH access to the systems. In such a case you will need to create a route and call the following line as below:
Route::get('/clear-cache', function() {
Artisan::call('cache:clear');
return "All cache cleared";
});
A short solution:
use Dotenv;
with(new Dotenv(app()->environmentPath(), app()->environmentFile()))->overload();
with(new LoadConfiguration())->bootstrap(app());
In my case I needed to re-establish database connection after altering .env programmatically, but it didn't work , If you get into this trouble try this
app('db')->purge($connection->getName());
after reloading .env , that's because Laravel App could have accessed the default connection before and the \Illuminate\Database\DatabaseManager needs to re-read config parameters.
In case anybody stumbles upon this question who cannot reload their webserver (long running console command like a queue runner) or needs to reload their .env file mid-request, i found a way to properly reload .env variables in laravel 5.
use Dotenv;
use InvalidArgumentException;
try {
Dotenv::makeMutable();
Dotenv::load(app()->environmentPath(), app()->environmentFile());
Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
//
}
This is the only question I could find relating to reloading .env and thus config($key) values on an existing app instance, but many of these answers should probably live on a different question.
I got the following to work for a Laravel 6.x application, while trying to make an artisan command use my cypress environment variables.
For context $this->laravel === app().
/**
* Make the application use the cypress environment variables
*
* #return void
*/
protected function enableCypressEnv()
{
// Get all of the original values from config. We need to do this because
// rebuilding config will skip packages.
$old = app(Repository::class)->all();
// Change the applications env file
$this->laravel->loadEnvironmentFrom('.env.cypress');
// Reload the app's environment variables
Dotenv::create(
$this->laravel->environmentPath(),
$this->laravel->environmentFile(),
Env::getFactory()
)->overload();
// Force config to be rebuitl with new env
(new LoadConfiguration())->bootstrap($this->laravel);
// Get all of the new values from buidling with new env
$new = app(Repository::class)->all();
// Merge the new values over the old distinctly
$merged = array_merge_recursive_distinct($old, $new);
// Get the applications configuration instance
$config = config();
// Overwrite all of the values in the container in accordance with the merged values
foreach ($merged as $key => $value) {
$config->set([$key => $value]);
}
}
Shoutout to the answers above for pointing me in the right direction, but I will include my answer here as none of them worked perfectly for me.
In config/database.php I changed the default DB connection from mysql to sqlite. I deleted the .env file (actually renamed it) and created the sqlite file with touch storage/database.sqlite. The migration worked with sqlite.
Then I switched back the config/database.php default DB connection to mysql and recovered the .env file. The migration worked with mysql.
It doesn't make sense I guess. Maybe was something serverside.
I want to select all data from 1 table in command prompt (not from browser) using Laravel framework. Is there anyone know what the good references is? please give me link, thankyou
You can create an Artisan Command.
Check the Laravel 5.0 docs:
http://laravel.com/docs/5.0/commands
Then, you can run you command. Example:
php artisan mycommand:list
Laravel 4.2:
http://laravel.com/docs/4.2/commands
You can run artisan commands from "cmd" (Windows). (Edit your environment variables first! Add you PHP path).
Try:
php artisan tinker
Then:
DB::table('users')->get()
when I trying to migrate something I get this error:
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
Command Cancelled!
Im running a centOS 6.5 server without Plesk 12
Is there anyway to figure out what the error is or how to solve it?
Thanks
A bit late for answer but just for info purpose you can use php artisan migrate --force to avoid it OR in laravel 5.2 they are using a key env in config/app.php configure it to avoid this prompt
reference https://github.com/laravel/laravel/blob/master/config/app.php
Just use --force flag in production:
php artisan migrate --force
https://laravel.com/docs/6.x/migrations#running-migrations
Forcing Migrations To Run In Production
Some migration operations are destructive, which means they may cause
you to lose data. In order to protect you from running these commands
against your production database, you will be prompted for
confirmation before the commands are executed. To force the commands
to run without a prompt, use the --force flag.
Change your environment first from production to local . /bootstrap/start.php somewhere line no 26
$env = $app->detectEnvironment(array(
// find your machine name and replace mine
'local' => array('hassanjamal.local'),
));
I've just bumped into this problem as well. If you don't want to reconfigure your app (which is probably a good idea) hit 'Y' and then enter instead of just enter. It will go on even in production setting.
Simple,
$env = $app->detectEnvironment(function()
{
return 'development';
});
Good luck!
I used to have a bootstrap/compiled.php file. Then I accidentally deleted it, and because for some reason it was in .gitignore, I don't have another copy of it. How do I get it back?
I tried running compose dump-autoload and php artisan optimize, neither of these generated the file. Where does it come from? How do I produce it?
Laravel 4.1+ does not create bootstrap/compiled.php on development boxes anymore, only in 'production'. Actually Laravel checks if debug mode is on if it is, it will not generate the compiled.php.
But you can:
php artisan optimize --force
To force its creation.
Switch debug mode to false in app/config/app.php and run your command again.
'debug' => false,