Laravel 5 - 未知数据库

4

我正在尝试在Laravel 5中创建登录表单。

每次尝试登录时,都会出现以下错误:

PDOException in Connector.php line 55: SQLSTATE[42000] [1049] Unknown database 'php2project'

但是数据库存在。 enter image description here

我可以无任何问题使用“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() !!}

2
你能发布 app/config/database.php 的内容吗? - Daan
你正在使用哪个编程环境? - sandeepsure
@Daan 更新了原帖 - user3589409
1
你是否通过 php artisan serve 运行此程序?并且在 serve 命令已经启动后,你是否对 .env 文件进行了任何更改? - Bogdan
1
我看到你正在使用Laravel的auth,你能否查看app/config/auth.php中的驱动程序是否设置为eloquent或database?如果是eloquent,那么你能发布User模型吗? - Daan
显示剩余9条评论
2个回答

1

首先检查.env文件中是否设置了正确的凭据。

如果是,则尝试缓存配置。有时,问题可能是由于您更改了.env文件但未缓存而导致的(仅当您在项目中缓存配置时)。

尝试运行php artisan cache:config命令。


-1

看起来你的数据库config->database.php中被设置为forge,而在.env文件中的数据库名称是php2project。确保两个文件中的数据库名称相同,同时password在config->database.php中为空,但在.env文件中设置为_STACK_,还要确保两个文件中的username相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接