I want to connect to another database sometimes.
I created a config.php with the database connection data.
But how can i tell laravel to connect to this database insted of using the config/database.php?
For example when using the Schema
class.
Since no one seems to understand what i want.
I DON'T want to use the config/database.php, i want to use a different config file on a different location.
In Lumen you can create a database connection by filling out the default information you can find in . env file. But if you need multiple database connections you can do so by adding a new database config file and creating a list of the new database connections. Create a database config file /config/database.
Laravel makes connecting with SQL Server database and running queries extremely simple out of the box. The SQL Server, MSSQL database configuration for your application is located at Laravel Project Root Folder, config/database.
DB::purge($name) ...which will clear the $connections property within Illuminate\Database\DatabaseManager for the specified connection.
It sounds like you figured this out. Here's how I'd accomplish it anyway for other people coming in, or in case something useful is here for you.
First, Add a second connection in app/config/database.php
. Note: That file path may change depending on your environment.
<?php return array( 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database1', 'username' => 'user1', 'password' => 'pass1' 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), 'mysql2' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database2', 'username' => 'user2', 'password' => 'pass2' 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), ), );
Second, in your code, you can use (as mentioned) the 2nd connection where you would like:
Schema::connection('mysql2')->create('users', function($table) {})
There's more documentation on this - see Accessing Connections.
Eloquent ORM You can define the variable for "connection" in an eloquent class to set which connection is used. That's noted in the Basic Usage section.
See that variable on here on Github and the method which you can set to set the connection dynamically here.
Edit The OP has made it clear that they do not wish to use the config/database.php file for config.
However without explaining further, I can't comment. I'm happy to help - sounds like it would be useful to know why the config/database.php file can't/shouldn't be used, as this can help us ascertain the problem and create a useful solution.
I believe you want to implement some kind of logical sharding where databases would be dynamically created.
In such scenario in laravel you can dynamically add a database config, like below
$conn = array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'DATABASE', 'username' => 'USERNAME', 'password' => 'SOME_PASSWORD', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ); Config::set('database.connections.DB_CONFIG_NAME', $conn);
Now to connect via eloquent
MODEL::on('DB_CONFIG_NAME')->WHAT_EVER('1');
Incase of Query Builder you can do
$DB = DB::connection('DB_CONFIG_NAME');
use $DB->select()
for querying now.
Hope this would help devs looking for a possible solution for this question
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