Clear Laravel cache(s) without Artisan command [duplicate] - php

This question already has answers here:
Laravel 5 – Clear Cache in Shared Hosting Server
(20 answers)
Closed 1 year ago.
I've moved my project but have run into an issue. I'm discovering that the old settings (which have been changed in my .env to reflect the changed host) are still being used by my project.
I can't access my project through a browser as it returns this error: SQLSTATE[HY000] [1045] Access denied for user 'appReop'#'localhost' (using password: YES) (SQL: select * from `settings` limit 1)
appReop was the previous login username. And while I could just create a new user to reflect this, I also run into an issue about the old password being too weak for the current database configuration.
When I go to run php artisan cache:clear, it returns the same above error. So I was curious if there was a way to manually clear the caches to fix my errors?
Update
This shows the three errors that pop up after running any Artisan command:

Not sure you have solved this, my suggestion is remove the files inside below folder. So the database connection cache file will create again after removed. (Laravel 5)
/<root>/bootstrap/cache/*
Hope it helps :)

You can call an Artisan command outside the CLI.
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('cache:clear');
// return what you want
});
You can check the official doc here http://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli

It's one time only? Just delete the folder storage/framework/cache/data

If someone still has such problem, check out your password for database. It seems like .env file does not accept # sign, that's why I had the same error.

rather batter if you could provide permission to your storage directory.
$ sudo chmod -R 777 ./storage
then remove cache and using php artisan cache:clear.I hope this will help you.

Try running:
php artisan down
php artisan config:clear
As this will place Laravel in maintenance mode and then clear only the config setting cache.
#Filippe commented about this already.
Update
Just create a file called down like in the picture below then run php artisan config:clear and then if it works php artisan up.

Related

Laravel registration error, database is connected, but receiving strange error

I have connected to my database successfully as I was able to do all of my beginning migrations, users table and password reset etc.
It is my first time running php artisan make:auth
and when I go to register a new user for the laravel application, I get this error.
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost'
(using password: YES) (SQL: select count(*) as aggregate from users
where email = ma#yahoo.com)
Here is my .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=larabiz
DB_USERNAME=root
DB_PASSWORD=
The answer was quite simple but weird.
run php artisan make:auth. then run php artisan migrate. shutdown your server, restart the server, and then register your first user. This worked for me, so Laravel was not able to add users during registration, without first restarting the server. That took me 5 fresh Laravel installs and three hours of work! I was in tears :).
The answer was quite simple but weird.
run php artisan make:auth.
then run php artisan migrate.
shutdown your server, restart the server, and then register your first user. This worked for me, so Laravel was not able to add users during registration, without first restarting the server. That took me 5 fresh Laravel installs and three hours of work! I was in tears :).
Did yout try to reload config ?
php artisan config:clear
or clear cache ?
php artisan cache:clear
Go to config/database.php and under connections set the values for your connection manually.

Laravel Artisan - reload .env variables or restart framework

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

How could I migrate my database with "php artisan migrate" in laravel

I clone my project from gitlab and I make it on my new computer.
But I get nothing ouput after I run php artisan migrate and the corresponding table is not created.
Output of php artisan migrate:status below.
What the meaning of "Ran?" and why status my migration file are all "N". php artisan migrate will create these tables successfully only when the "Ran?"s are "Y"?
An Y in the Ran column means the migration has been done.
Check your .ENV file , make sure your database settings are correct, also make sure you actually created the database beforehand.
clear the entire laravel cache to stay up to date with current .env settings, it might help.
try using php artisan migrate:refresh sometime it happens with me and i find out it's already migrated

Reloading .env variables without restarting server (Laravel 5, shared hosting)

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.

Laravel migration cancelled

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!

Categories