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
- Create a directory
Database
inside your app folder. - Place the
DbOnTheFly.php
inside that new folder.
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();
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();