我正在尝试在Laravel 5中创建登录表单。
每次尝试登录时,都会出现以下错误:
PDOException in Connector.php line 55: SQLSTATE[42000] [1049] Unknown database 'php2project'
我可以无任何问题使用“php artisan migrate”,所以我不知道为什么现在会出现这个问题。
这是我的.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
编辑:以下是我的配置/数据库信息
'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' => '',
],
],
控制器
<?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');
}
}
}
}
视图
@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' => 'john@snow.com')) !!}
</p>
<p>
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
</p>
<p>{!! Form::submit('Submit!') !!}</p>
{!! Form::close() !!}
app/config/database.php
的内容吗? - Daanphp artisan serve
运行此程序?并且在serve
命令已经启动后,你是否对.env
文件进行了任何更改? - Bogdanauth
,你能否查看app/config/auth.php
中的驱动程序是否设置为eloquent或database?如果是eloquent,那么你能发布User模型吗? - Daan