Currently i have a application that hosts multiple tenants written in CodeIgniter. But i am really loving Laravel 4 and i would like to start migrating the application to Laravel.
Here is the current setup:
In Codeigniter it's relatively easy to start and end new database connections.
With Laravel i have the following issues/questions.
These are my main issues, i have some other minor stuff but those can be worked around.
Hopefully someone can shed some light..
I'm just taking a stab at this, so be warned :) The DatabaseManager class, which is used whenever you call DB, has and extend method. Here's the link to the source. The DB::connection() method should return an instance of Illuminate\Database\Connection. From all this, I would create a new user connection as follows:
$user = Auth::user(); DB::extend($user->username, function() use ($user) { // $pdo = new PDO(); set this up how you see fit return new Illuminate\Database\Connection($pdo, $user->databaseName, $tablePrefix); });
Personally, I would add a new method to each user, User::databaseConnection(), and call that when I extend the DatabaseManager.
DB::extend($user->username, function() use ($user) { return $user->databaseConnection(); });
Through out your application you should be able to call a registered user's connection via:
DB::connection(Auth::user()->username);
Update
Depending on how often and when you'd be calling the tenant connection, you may want to use the IOC container.
App::bind('tenantDB', function() { return DB::connection(Auth::user()->username); }); App::make('tenantDB')->insert(...);
I forgot about migrations and seeding. For migrations, you can set the file path
php artisan migrate:make foo --path=app/migrations
So if you use the Config class to set the default database or DB::setDefaultConnection($username), I'd assume all migrations and seeding will be done for the current connection. When that process if complete you can switch back to you main database.
Update 2
The laravel developers are amazing and I should have definitely got the urge to check this out sooner than later. You can do migrations and seed on any database connection you've created.
artisan migrate --database='userConnectionName' artisan db:seed --database='userConnectionName'
Looking at Barry's answer, that is probably a good deal simpler than extending the DatabaseManager.
If you want to see all the options for these commands just run:
artisan help migrate artisan help db:seed
You can create 1 database with the tenant database credentials, and dynamically set them in your app:
$tenant = Tenant::where('username', '=', $username)->first(); Config::set('database.connections.tenant.username', $tenant->db_username); Config::set('database.connections.tenant.password', $tenant->db_password); Config::set('database.connections.tenant.database', $tenant->db_database);
This will require to create 2 connections in your database.php file. (for instance app and tenant) and specify in your model which database to use (1 for the storing of tenants, 1 for the tenant specific database)
And probably create a route/script to create/update the tables. Not sure about migrations with multiple databases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With