Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add DB::build method #53464

Merged
merged 2 commits into from
Nov 11, 2024
Merged

[11.x] Add DB::build method #53464

merged 2 commits into from
Nov 11, 2024

Conversation

stevebauman
Copy link
Contributor

Description

Similar to #53411, this PR implements a new DB::build method that allows you to create new DB connections that are not defined in your configuration file.

Usage

use Illuminate\Support\Facades\DB;

$sqlite = DB::build([
    'driver' => 'sqlite',
    'database' => ':memory:',
]);

$mysql = DB::build([
    'driver' => 'mysql',
    'database' => 'forge',
    'username' => 'root',
    'password' => 'secret',
]);

$pgsql = DB::build([
    'driver' => 'pgsql',
    // ...
]);

$sqlsrv = DB::build([
    'driver' => 'sqlsrv',
    // ...
]);

@henzeb
Copy link
Contributor

henzeb commented Nov 10, 2024

What is wrong with addConnection ?

@taylorotwell taylorotwell merged commit fc04bbf into laravel:11.x Nov 11, 2024
33 checks passed
@adiramardiani
Copy link

What is wrong with addConnection ?

to be honest, this feature is what I want for so long, this mechanism allow us freely change connection especially who using multi tenant or whatever

for example db_company_a.document_transactions, db_company_b.document_transactions, user who make API request to laravel who send info about (hashed) database_name, can use this method

but @stevebauman can you give the best practice to use this case, I don't know the best practice, is using middleware, trait in model, or whatever to match my case ? maybe you can make tweet thread or something about your new feature hehehe

@stevebauman
Copy link
Contributor Author

stevebauman commented Nov 14, 2024

@adiramardiani Imo this should really only be used in niche cases where you need a temporary DB connection.

For example, you could create a temporary DB connection to a SQLite file that a user uploads to your application:

$path = $request->file('database')->store();

$sqlite = DB::build([
    'driver' => 'sqlite',
    'database' => $path,
]);

$results = $sqlite->table('...')
    ->select([...])
    ->get();

Or say you have an intranet enterprise application that allows you to import data from various databases stored in your database:

use App\Models\User;
use App\Models\Database;

$database = Database::first();

$connection = DB::build([
    'driver' => $database->driver,
    'host' => $database->host,
    'port' => $database->port,
    'username' => $database->username,
    'password' => $database->password,
]);

// Sync external DB users...
$connection->table('users')->each(function ($user) {
    User::firstOrCreate([...]);
});

Or maybe you need a temporary in-memory SQLite instance for testing:

$db = DB::build([
    'driver' => 'sqlite',
    'database' => ':memory:',
]);

$db->table('...')->insert([...]);

$db->table('...')->where('...')->get();

Just some thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants