1

I am trying to write an installer-type app using Laravel 10. Where the purpose is: if I don't set the database information it will redirect me to the setup database. When I fill up the form and submit, it performs the following task:

  1. Update database-related content in the .env file
  2. Clear cache, config, and finally cache new config using the Artisan call.
  3. Finally migrate the database still using the Artisan call.

Although the .env file content is updated, it still uses the last database-related info that existed before the update. I mean, suppose previously DB name was ab_setup, then I update it to nn_setup, .env file shows the DB name as nn_setup, but the browser responds:

Unknown database ab_setup (Connection: mysql, SQL: select * from information_schema.tables where table_schema = nn_setup and table_name = migrations and table_type = 'BASE TABLE').

I have no idea what's actually wrong. Here is my code:

// Update .env file content
$envContent = [
    'DB_CONNECTION' =>  $request->database_connection,
    'DB_HOST'       =>  $request->database_host,
    'DB_PORT'       =>  $request->database_port,
    'DB_DATABASE'   =>  $request->database_name,
    'DB_USERNAME'   =>  $request->database_username,
    'DB_PASSWORD'   =>  $request->database_password
];

foreach( $envContent as $key => $value ) {
    $this->replace_env_value($key, $value);
}

// Clear Cache and Config & Cache new Config
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');

// Migrate DB and Seed
Artisan::call('migrate');
Artisan::call('db:seed');

// Create Admin User
$user = new User();
$user->name = $request->admin_name;
$user->email = $request->admin_username;
$user->password = Hash::make($request->admin_password);
$user->save();

Does anyone have the idea what I miss that will fix the issue?

2
  • 1
    .env values are passed to Laravel's config(), like config('database'); I wonder if that is being "sticky", despite the call to config:clear 🤔 If you do dd(config('database.connections.' . config('database.default'))) following that artisan call, do you see the old or new values? Commented Feb 23, 2023 at 17:50
  • Thanks for your idea, unfortunately, it doesn't update the value in config. Commented Feb 24, 2023 at 5:26

1 Answer 1

1

You should reconnect the database manually. Reference here.

Also, you need to remove cache before with DB:purge() method

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works. But now I face another issue. After the operation, the redirection should be <ROOT_URL>/login based on this code return redirect()->route('login'); but unfortunately, it tries to redirect in localhost/login
Did you check APP_URL in the .env file? Also, you can set base url in config/app.php file. 'url' => 'example.com',
I solve this in an alternative way. But there is another issue. After successfully completing the action of the db_installation method, it redirects to the login page. When I tried to log in, interestingly the login page got the old DB configuration. I mean I performed DB::disconnect();, DB::purge();, and Config::set('database.connections.mysql', [all_db_info_set_here]); which successfully create related tables and insert data in the new record. But when I use dd(config('database.connections.mysql')); before redirect to login page, surprisingly it prints old DB info!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.