Laravel 5.1 如何设置数据库、服务器、用户名和密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31760380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:06:01  来源:igfitidea点击:

How to set database, server, username and password in Laravel 5.1?

phplaravellaravel-5laravel-5.1

提问by scott

I am trying to set the database name, server name, username and password in Laravel via an html form (similar to the way that this can be done in WordPress). Is there any way to do this with Laravel?

我正在尝试通过 html 表单在 Laravel 中设置数据库名称、服务器名称、用户名和密码(类似于在 WordPress 中可以完成的方式)。有没有办法用 Laravel 做到这一点?

回答by Disha V.

It is possible to do it in 2 ways in Laravel:

在 Laravel 中可以通过两种方式做到这一点:

1) Go to <projectname>/config/database.phpand search for 'connections' =>

1) 前往<projectname>/config/database.php并搜索'connections' =>

2) Set it in .envfile on the root.

2) 将其设置在.env根文件中。

回答by cre8

You can write your own AdminPanel where you get all config values from the .env file, put them in a form and write them back to the file. Make sure to delete the AdminPanel after the configuration because it's a big security issue.

您可以编写自己的 AdminPanel,从中获取 .env 文件中的所有配置值,将它们放入表单中并将它们写回文件。确保在配置后删除 AdminPanel,因为这是一个很大的安全问题。

回答by S.Alireza

  1. go to .env file in the root of your project (must enable show hidden file)
  2. if you are using mySql database find these three line or if not exist add this three line into .env file
    • DB_DATABASE=your_database_name
    • DB_USERNAME=your_database_username
    • DB_PASSWORD=your_database_password
  1. 转到项目根目录中的 .env 文件(必须启用显示隐藏文件)
  2. 如果您使用的是 mySql 数据库,请找到这三行,如果不存在,请将这三行添加到 .env 文件中
    • DB_DATABASE=your_database_name
    • DB_USERNAME=your_database_username
    • DB_PASSWORD=your_database_password

for example :

例如 :

  • DB_DATABASE=college
  • DB_USERNAME=root
  • DB_PASSWORD=password
  • DB_DATABASE=大学
  • DB_USERNAME=root
  • DB_PASSWORD=密码

after doing these work, run this command again "php artisan serve"

完成这些工作后,再次运行此命令“php artisan serve”