2016-08-17 16 views
1

私はルーメンベースのクエリをlocalhostで実行しようとしています。適切なデータベース名を正しく呼び出す方法がわかりません。PHP Laravel Lumenでデータベースへの接続を確立するには?

EDIT:私は以下のエラーを取得しています理由は、私のプロジェクトで私の.envファイル内のDB名の名前です。私の.envファイルのDB_DATABASE=mydbschemaname行にデータベース名が必要ですが、それをどのように見つけることができますか?私はどこでもそれを見つけることができません。

database error

次のように私が持っているコードは、APP->のHttpに位置routes.phpある:APP-> HTTP->コントローラで

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

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アプリ - > 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->ルーメン-framework->設定:

<?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), 
     ], 

    ], 

]; 

最後に、ここではappフォルダの外に位置私.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 

私はDB_DATABASEの名前を変更する必要があるのが最後のファイル(.env)だと思っていますが、私は完全にはわかりません。どんな助けもありがとう。

FYI:ここでの最終結果は、データベースに接続して、ユーザーテーブルに追加するユーザーのレコードを表示できるようにすることです。

+0

はい、データベースへの接続情報は、 '.env'ファイルで設定する必要があります。 – patricus

+0

@patricus私はそれを理解しています。私は私のdbの名前を知らないので、私は接続することはできません。これを見つける方法はありますか? – NoReceipt4Panda

+0

あなたはデータベースの作成を担当します。データベースを作成したら、 '.env'ファイルを接続情報(たとえば、作成したデータベースの名前)で更新し、移行を実行して(' php artisan migrate')データベース内にテーブルを作成します。 – patricus

答えて

1

スキーマ名が「データベース」のデータベースはありますか?

データベースが設定されているので、以下のようにDB_プロパティを変更する必要があります。

など。

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コマンドラインを使ってDBを作成できますか?または、それを達成するためにmySQL Managementスタジオのようなソフトウェアを使用する必要がありますか? – NoReceipt4Panda

関連する問題