Laravel 5 - 未知数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33080902/
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
Laravel 5 - Unknown Database
提问by
I'm trying to make a login form in laravel 5.
我正在尝试在 laravel 5 中创建一个登录表单。
Everytime I try to log in I get the error
每次我尝试登录时都会出现错误
PDOException in Connector.php line 55: SQLSTATE[42000] [1049] Unknown database 'php2project'
Connector.php 第 55 行中的 PDOException:SQLSTATE[42000] [1049] 未知数据库 'php2project'
I could use "php artisan migrate" without any problem, so I have no idea why I'm having this issue right now.
我可以毫无问题地使用“php artisan migrate”,所以我不知道为什么我现在遇到这个问题。
here's how my .env file looks
这是我的 .env 文件的外观
APP_ENV=local
APP_DEBUG=true
APP_KEY=_STACK_
DB_HOST=127.0.0.1
DB_DATABASE=php2project
DB_USERNAME=root
DB_PASSWORD=_STACK_
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
EDIT: Here is my config/database information
编辑:这是我的配置/数据库信息
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
Controller
控制器
<?php
namespace App\Http\Controllers;
use View;
use Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
class LogInController extends Controller
{
public function showLogin()
{
return View::make('login.login');
}
public function doLogin()
{
// validate
$rules = array(
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
// create our user data for the authentication
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful
return Redirect::to('login');
}
}
}
}
view
看法
@extends('layouts.master')
@section('title', 'Login')
@section('content')
{!! Form::open(array('url' => 'login')) !!}
<h1>Login</h1>
<p>
{!! $errors->first('email') !!}
{!! $errors->first('password') !!}
</p>
<p>
{!! Form::label('email', 'Email Address') !!}
{!! Form::text('email', Input::old('email'), array('placeholder' => '[email protected]')) !!}
</p>
<p>
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
</p>
<p>{!! Form::submit('Submit!') !!}</p>
{!! Form::close() !!}
回答by Bhaskar Dabhi
First check if Correct Credentials are set in .evn
首先检查 .evn 中是否设置了正确的凭据
If yes then try caching the config. Sometime issue may occur because you made change in .env but didnt cache it(Only if you are caching configs in your project).
如果是,则尝试缓存配置。有时可能会出现问题,因为您在 .env 中进行了更改但没有缓存它(仅当您在项目中缓存配置时)。
try by running php artisan cache:config
尝试运行 php artisan cache:config
回答by Saurab
It seems like your databaseis set to forgein config->database.phpwhereas database name in .envfile is php2project. Make sure they are same in both files, also passwordis blank in config->database.php but is set to _STACK_ in .env file and also make sure that your usernameis same in both files.
似乎您的数据库设置为在config->database.php 中伪造,而.env文件中的数据库名称是php2project。确保它们在两个文件中相同,密码在 config->database.php 中也为空,但在 .env 文件中设置为 _STACK_ 并确保您的用户名在两个文件中相同。