Skip to content

Instantly share code, notes, and snippets.

@digitalkreativ
Last active April 11, 2021 10:57
Show Gist options
  • Save digitalkreativ/3eb197e39a5541f7c907b6cbdbfccb2d to your computer and use it in GitHub Desktop.
Save digitalkreativ/3eb197e39a5541f7c907b6cbdbfccb2d to your computer and use it in GitHub Desktop.
Lumen 5 - On the fly Database connection #lumen

Description

Enable on-the-fly database connection on the Lumen framework. This is based on the OTF class made by @lukevers

https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5 https://gist.github.com/lukevers/2e40dc2dc0cf4818b1ba#file-otf-php

Installation

  • Create a directory Database inside your app folder.
  • Place the DbOnTheFly.php inside that new folder.

Basic Usage

Use the same connection details as your normal connection but change the database name. Your database user needs access to this new database of course.

$otf = new App\Database\DbOnTheFly(['database' => '<the-other-database>']);

// Get the users table
$users = $otf->getTable('<the-user-table>');

// Find the first user in the table
$first_user = $users->first();

Advanced usage

Change more settings of the connection. These are the options like you have in your normal configuration file vendor/laraval/lumen-framework/config/database.php .

All options except database are optional.

$otf = new App\Database\DbOnTheFly([
    'driver'   => 'your-driver',
    'database' => 'your-database',
    'host'     => 'your-host',
    'username' => 'your-user',
    'password' => 'your-password'
] );


// Get the users table
$users = $otf->getTable('<the-user-table>');

// Find the first user in the table
$first_user = $users->first();
<?php
/*******
* Class based on the OTF class made by @lukevers
* https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5
* https://gist.github.com/lukevers/2e40dc2dc0cf4818b1ba#file-otf-php
*/
namespace App\Database;
/**
* Class DbOnTheFly
* @package App\Database
*/
class DbOnTheFly
{
/**
* 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'] : app('config')->get("database.default");
$default = app('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
app('config')->set("database.connections.$database", $default);
// Create the connection
$this->connection = app('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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment