Laravel migration cancelled - php

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!

Related

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

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.

How to run laravel migrations without artisan (using code)

I recently hosted a laravel project (for a customer) on shared hosting, after failed attempts to get access to the server via ssh I contacted the host who informed me that ssh service was not available for my customers hosting plan, that means I have no access to terminal and can't use artisan. I know how to write a php script that will create sql tables but just before that I was wondering if theres a shortcut to this with laravel since the migrations(tables) are already defined. What I want is like to create a route project.com/run_migrations to do the job! Thanks in advance
You can easily create a small Artisan script within PHP like this:
Artisan::call('migrate');
This equals php artisan migrate. Use it anywhere you want to run your migrations.
If you are in production mode (if APP_ENV=production inside your .env file) then you would have to force the migration in case you want to allow to make changes. You can do it as follows:
Artisan::call('migrate', ["--force" => true ]);
This equals adding the --force flag a la php artisan migrate --force.
To answer your specific question though, create a route like this:
Route::get('/run-migrations', function () {
return Artisan::call('migrate', ["--force" => true ]);
});
If you are interested in creating a web installer, you might be interested in this package:
https://github.com/Froiden/laravel-installer
Check out the code to see how he handles migrations and seeds etc.

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

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 artisan optimize not creating bootstrap/compiled

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,

Categories