2016-08-17 86 views
1

我試圖通過localhost運行基於流明的查詢。我不知道如何正確調用正確的數據庫名稱。如何在PHP Laravel Lumen中建立與數據庫的連接?

編輯:我收到以下錯誤的原因是因爲數據庫名稱的在我的項目我的.env文件的名稱。我的.env文件中的DB_DATABASE=mydbschemaname行需要有我的數據庫名稱,但是如何找到該文件?我無法在任何地方找到它。

database error

代碼我有如下,routes.php位於APP->的Http:

$app->get('/records', '[email protected]'); 

在APP-> HTTP- UserController.php>控制器:

namespace App\Http\Controllers; 

use App\User; 
use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 

class UserController extends Controller 
{ 

    public function index() { 
     $users = User::all(); 
     return response()->json($users); 
    } 
} 

User.php in app-> Http:

namespace App; 

use Illuminate\Auth\Authenticatable; 
use Laravel\Lumen\Auth\Authorizable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; 

class User extends Model implements 
    AuthenticatableContract, 
    AuthorizableContract 
{ 
    use Authenticatable, Authorizable; 

    protected $fillable = [ 
     'name', 'email', 
    ]; 

    protected $hidden = [ 
     'password', 
    ]; 
} 

在APP->數據庫 - >遷移[date]_create_users_table.php

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateUsersTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name', 200); 
      $table->string('email', 200)->unique(); 
      $table->string('password', 200); 
      $table->timestamps(); 
     }); 

     DB::table('users')->insert(
      ['id' => 1, 'name' => 'example', 'email' => '[email protected]', 'password' => 'thisisthepassword', 'updated_at' => '2015-10-15 01:23:45', 'created_at' => '2015-10-15 01:23:45'] 
     ); 
    } 

    public function down() 
    { 
     Schema::drop('users'); 
    } 
} 

然後當然,我有位於所述database.php APP->於供應商> laravel->內腔框架 - >配置:

<?php 

return [ 

    /* 
    |-------------------------------------------------------------------------- 
    | PDO Fetch Style 
    |-------------------------------------------------------------------------- 
    | 
    | By default, database results will be returned as instances of the PHP 
    | stdClass object; however, you may desire to retrieve records in an 
    | array format for simplicity. Here you can tweak the fetch style. 
    | 
    */ 

    'fetch' => PDO::FETCH_CLASS, 

    /* 
    |-------------------------------------------------------------------------- 
    | Default Database Connection Name 
    |-------------------------------------------------------------------------- 
    | 
    | Here you may specify which of the database connections below you wish 
    | to use as your default connection for all database work. Of course 
    | you may use many connections at once using the Database library. 
    | 
    */ 

    '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' => [ 

     'testing' => [ 
      'driver' => 'sqlite', 
      'database' => ':memory:', 
     ], 

     'sqlite' => [ 
      'driver' => 'sqlite', 
      'database' => env('DB_DATABASE', base_path('database/database.sqlite')), 
      'prefix' => env('DB_PREFIX', ''), 
     ], 

     'mysql' => [ 
      'driver' => 'mysql', 
      'host'  => env('DB_HOST', 'localhost'), 
      'port'  => env('DB_PORT', 3306), 
      'database' => env('DB_DATABASE', 'forge'), 
      'username' => env('DB_USERNAME', 'forge'), 
      'password' => env('DB_PASSWORD', ''), 
      'charset' => env('DB_CHARSET', 'utf8'), 
      'collation' => env('DB_COLLATION', 'utf8_unicode_ci'), 
      'prefix' => env('DB_PREFIX', ''), 
      'timezone' => env('DB_TIMEZONE', '+00:00'), 
      'strict' => env('DB_STRICT_MODE', false), 
     ], 

     'pgsql' => [ 
      'driver' => 'pgsql', 
      'host'  => env('DB_HOST', 'localhost'), 
      'port'  => env('DB_PORT', 5432), 
      'database' => env('DB_DATABASE', 'forge'), 
      'username' => env('DB_USERNAME', 'forge'), 
      'password' => env('DB_PASSWORD', ''), 
      'charset' => env('DB_CHARSET', 'utf8'), 
      'prefix' => env('DB_PREFIX', ''), 
      'schema' => env('DB_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' => env('DB_CHARSET', 'utf8'), 
      'prefix' => env('DB_PREFIX', ''), 
     ], 

    ], 

    /* 
    |-------------------------------------------------------------------------- 
    | Migration Repository Table 
    |-------------------------------------------------------------------------- 
    | 
    | This table keeps track of all the migrations that have already run for 
    | your application. Using this information, we can determine which of 
    | the migrations on disk haven't actually been run in the database. 
    | 
    */ 

    'migrations' => 'migrations', 

    /* 
    |-------------------------------------------------------------------------- 
    | Redis Databases 
    |-------------------------------------------------------------------------- 
    | 
    | Redis is an open source, fast, and advanced key-value store that also 
    | provides a richer set of commands than a typical key-value systems 
    | such as APC or Memcached. Laravel makes it easy to dig right in. 
    | 
    */ 

    'redis' => [ 

     'cluster' => env('REDIS_CLUSTER', false), 

     'default' => [ 
      'host'  => env('REDIS_HOST', '127.0.0.1'), 
      'port'  => env('REDIS_PORT', 6379), 
      'database' => env('REDIS_DATABASE', 0), 
      'password' => env('REDIS_PASSWORD', null), 
     ], 

    ], 

]; 

最後,這裏是我的.env文件所在的文件夾的應用程序之外,在根項目文件夾:

APP_ENV=local 
APP_DEBUG=true 
APP_KEY=SomeRandomKey!!! 

APP_LOCALE=en 
APP_FALLBACK_LOCALE=en 

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_PORT=3306 
DB_DATABASE=database 
DB_USERNAME=homestead 
DB_PASSWORD=secret 

CACHE_DRIVER=memcached 
SESSION_DRIVER=memcached 
QUEUE_DRIVER=database 

我假設這是最後一個文件(.env),我必須更改DB_DATABASE名稱,但我不完全確定。任何幫助,將不勝感激。

僅供參考:此處的最終結果是能夠連接到數據庫並顯示我在用戶表中添加的用戶的記錄。

+0

是的,你需要在'.env'文件中設置連接信息到你的數據庫。 – patricus

+0

@patricus我明白這一點。我不知道我的數據庫的名稱,因此我無法連接。有沒有辦法找到這個? – NoReceipt4Panda

+0

您有責任創建數據庫。創建數據庫後,使用連接信息(例如您創建的數據庫的名稱)更新'.env'文件,然後運行遷移('php artisan migrate')在數據庫中創建表。 – patricus

回答

1

你確定你有架構名稱爲'database'的數據庫嗎?

您需要在設置數據庫後更改DB_屬性。

E.g.

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_PORT=3306 
DB_DATABASE=mydbschemaname 
DB_USERNAME=dbadmin 
DB_PASSWORD=myverysecretpassword 
+0

不幸的是,這完全沒有幫助,因爲我很困惑在哪裏/如何找到數據庫的名稱。這最終是阻礙我的東西。 – NoReceipt4Panda

+0

我只舉例。你需要配置你自己的數據庫。閱讀關於數據庫,MySQL(或其他),創建自己的,命名它,並將名稱放入'.env'配置文件。 – miikes

+0

我的問題是,我可以通過GIT bash命令行創建數據庫嗎?還是我需要使用像mySQL管理工作室這樣的軟件來實現? – NoReceipt4Panda

相關問題