2017-08-24 6 views
1

私はLumenにリクエストURLに基​​づいて複数のデータベースを扱うべき小さなアプリケーションを持っています。これを可能にするために、私は/config/というフォルダを作成し、/config/database.phpという設定ファイルを作成しました。ここにコードがあります。私は正確に何をする必要があるかを示すためにいくつかのコメントを追加しました。Call Config ::ルーメン5.4のデータベースを変更するように設定しますか? (Eloquentを使用して)

<?php 
return [ 
    'migrations' => 'home', 
    'default' => 'home', 
    'connections' => [ 
     // This is the default option and its needed to check if the "client" exists. 
     'home' => [ 
      'driver' => 'mysql', 
      'host'  => 'localhost', 
      'database' => 'home', 
      'username' => 'root', 
      'password' => 'password', 
      'charset' => 'utf8', 
      'collation' => 'utf8_unicode_ci', 
      'prefix' => '', 
      'strict' => false, 
     ],  

     // When the client is found in the "home" connection, I'd like to continue with this database settings as default. 
     'client' => [ 
      'driver' => 'mysql', 
      'host'  => 'localhost', 
      'database' => 'client_', // There is one database per client which in the end will look like this: "client_******", so I have to add this using Config:set... 
      'username' => 'root', 
      'password' => 'password', 
      'charset' => 'utf8', 
      'collation' => 'utf8_unicode_ci', 
      'prefix' => '', 
      'strict' => false, 
     ], 
    ], 
]; 

私は、これらの設定を変更するConfig::setを呼び出す必要がありますが、ルーメンに利用可能であるように見えるdoes notの。私はこれらの設定を変更するにはどうすればよい(デフォルトのデータベース接続とそのデータベース)私はこれを可能にする方法がある願ってい

。おかげでたくさん:)次のように使用することができ、クエリで

答えて

1

解決策が見つかりました。

設定を設定および編集するには、配列を引数として持つ "config"ヘルパーメソッドを使用します。

例:

config(['database.default' => 'your_connection']); 
0

$ connectionClient = DB ::接続( 'クライアント'); $ connectionHome = DB :: connections( 'home');

私は助けてくれることを願っています。

+0

こんにちはリカルド。ご回答有難うございます。これは素晴らしいですが、私はまだすべてのクライアントが自分のデータベースを持っているので、 "クライアント_ ****"に接続 "クライアント"のデータベースを変更する必要があります。どのようにデータベースを動的に設定できるか知っていますか? – KoalaGangsta

関連する問題