Al Bundy
Al Bundy

Reputation: 43

Laravel changing Database

I'm rather new to Laravel 4. What I have is a bunch of sports statistics information and for organization purposes I have a database for every sport (NFL, MLB, etc...). What I want to do is change the DB easily in queries and mimic the functionality of $myslqi->select_db() but aside from setting up a bunch of database connections in the config file, I can't find a way to do what I'm looking for. It's all the same connection and same user, I just want to be able to switch the DB without having to insert a variable into SQL in order to point to the right database.

Upvotes: 0

Views: 189

Answers (1)

alou
alou

Reputation: 1502

You will need to define the connections in the config/database like so:

'nba' => array(
        'driver'    => 'mysql',
         ...
         ),
'nfl' => array(
        'driver'    => 'mysql',
         ...
         ),

And then use these in models schema or queries

 class NbaPlayers extends Eloquent {
   protected $connection = 'nba';
   //other stuff on your model
 }



 $nflplayers = DB::connection('nfl')->whatever

If you define the connection in the model, you will use model::all() or whatever without having to define the connection everytime

Upvotes: 1

Related Questions