Laravel – 如何从 .env 文件中加载更新后的数据库连接信息?

huangapple go评论67阅读模式
英文:

Laravel - How to load updated database connection information from .env file?

问题

我正在尝试使用Laravel 10编写一个安装程序类型的应用程序。其目的是:如果我没有设置数据库信息,它将重定向我到设置数据库页面。当我填写表单并提交时,它执行以下任务:

  1. 更新.env文件中与数据库相关的内容。
  2. 清除缓存、配置,最后使用Artisan调用缓存新的配置。
  3. 最后使用Artisan调用迁移数据库。

尽管.env文件的内容已经更新,但它仍然使用更新之前存在的最后一个与数据库相关的信息。也就是说,假设之前的数据库名称是ab_setup,然后我将其更新为nn_setup,.env文件显示数据库名称为nn_setup,但浏览器响应如下:

未知数据库 ab_setup (连接:mysql,SQL:select * from information_schema.tables where table_schema = nn_setup and table_name = migrations and table_type = 'BASE TABLE')。

我不知道到底出了什么问题。以下是我的代码:

// 更新.env文件内容
$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);
}

// 清除缓存和配置,然后缓存新的配置
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');

// 迁移数据库和填充数据
Artisan::call('migrate');
Artisan::call('db:seed');

// 创建管理员用户
$user = new User();
$user->name = $request->admin_name;
$user->email = $request->admin_username;
$user->password = Hash::make($request->admin_password);
$user->save();

有没有人知道我漏掉了什么,可以解决这个问题?

英文:

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?

答案1

得分: 1

你应该手动重新连接数据库。参考这里

另外,在使用DB:purge()方法之前,你需要清除缓存。

英文:

You should reconnect the database manually.
Reference here.

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

huangapple
  • 本文由 发表于 2023年2月24日 01:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定