Skip to content

Instantly share code, notes, and snippets.

@lukevers
Last active October 5, 2020 22:19
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save lukevers/2e40dc2dc0cf4818b1ba to your computer and use it in GitHub Desktop.
Save lukevers/2e40dc2dc0cf4818b1ba to your computer and use it in GitHub Desktop.
Laravel 5 On The Fly Database Connections

This is a Laravel 5 class that I have at the location app/Database/OTF.php and use to connect to different databases on the fly.

// Using the same host/password/etc, just changing databases
$otf = new App\Database\OTF(['database' => 'kittens']);
// Get the users table
$users = $otf->getTable('users');
// Find the first user in the table
$first_user = $users->first();

Blog post here.

<?php namespace App\Database;
use Config;
use DB;
class OTF {
/**
* The name of the database we're connecting to on the fly.
*
* @var string $database
*/
protected $database;
/**
* The on the fly database connection.
*
* @var \Illuminate\Database\Connection
*/
protected $connection;
/**
* Create a new on the fly database connection.
*
* @param array $options
* @return void
*/
public function __construct($options = null)
{
// Set the database
$database = $options['database'];
$this->database = $database;
// Figure out the driver and get the default configuration for the driver
$driver = isset($options['driver']) ? $options['driver'] : Config::get("database.default");
$default = Config::get("database.connections.$driver");
// Loop through our default array and update options if we have non-defaults
foreach($default as $item => $value)
{
$default[$item] = isset($options[$item]) ? $options[$item] : $default[$item];
}
// Set the temporary configuration
Config::set("database.connections.$database", $default);
// Create the connection
$this->connection = DB::connection($database);
}
/**
* Get the on the fly connection.
*
* @return \Illuminate\Database\Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* Get a table from the on the fly connection.
*
* @var string $table
* @return \Illuminate\Database\Query\Builder
*/
public function getTable($table = null)
{
return $this->getConnection()->table($table);
}
}
@harrisoftware
Copy link

i'm stumped.
I'm hoping you have some ideas.

$otf = new App\Database\OTF([
    'driver'   => 'mysql',
    'database' => 'db_manufact',
    'username' => 'manufact',
    'password' => 'x'] );

$link = $otf->getTable('proflow');

$items = $link->first();

this works and I get:

{#460 ▼
+"code": 1
+"controlcode": 1
+"quantity": 25
+"priority": 5
+"location": 1
+"status": 5
+"operator": 60
+"entrydatetime": "2011-05-18 11:19:17"
+"comments": ""
+"exitdatetime": null
+"timdiff": 0
}

awesome right?
Other functions works too like $link->count() and $link->pluck('code')

But functions to get more rows or all rows yields a PDO exception:

things like:

$items = $link->take(10);

or

$items=$link->where('code','>',0)-get();

or

$items=$link->get();

Am I daft?

Thanks in advance.

PS: I'm developing OSX El Capitan. Although the error is a PDO exception, its being reported back to the browser as a proxy
Warren

@lukevers
Copy link
Author

lukevers commented Dec 8, 2015

Hey @harrisoftware, I'm actually not sure why ->get() would not work for you. I just tried both ->get() and ->take(10)->get() and they both work fine for me.

I do notice an error in your code block, but that might have just been a typo in your comment:

- $items=$link->where('code','>',0)-get();
+ $items=$link->where('code','>',0)->get();
                                    ^

If you're receiving a PDO exception I'd say look into that and see what the problem is and then try again!

@digitalkreativ
Copy link

Hi @lukevers

I had a need for something similar but then for the Lumen framework. You can find that code here:

https://gist.github.com/digitalkreativ/3eb197e39a5541f7c907b6cbdbfccb2d

I have of course linked to your blog and this gist in my gist :)

@Jeff-P
Copy link

Jeff-P commented Dec 6, 2018

awesome! Thank you very much

@Jeff-P
Copy link

Jeff-P commented Feb 8, 2019

For anyone needing to disconnect you can add another method and pass the db name back into the method.

	public function closeConnection($database)
	{
		$this->connection = DB::connection($database)->disconnect();
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment