Support the ongoing development of Laravel.io →
posted 7 years ago
Configuration
Last updated 2 years ago.
0

I would override config.database.default in middleware

0

I searched for 'config.database' and 'config.database.config', but nothing found, do you mean I can create .domain1.env, .domain2.env? Then crate request over custom middleware if domain is 1 or 2?

0

If I'm not wrong here, you want to run single app with multiple DB? If yes, see below solution.

Stuffs

You can configure multiple db into your laravel specified by different domain
1) Configure database settings

//app/config/database.php
return array(
  'default' => 'mysql1',
  'connections' => array(
    # Your primary database connection
    'mysql1' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Your secondary database connection
    'mysql2' => array(
        'driver'    => 'mysql',
        'host'      => 'host2',
        'database'  => 'database2',
        'username'  => 'user2',
        'password'  => 'pass2'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
  ),
);

2) Setup ConfigServiceProvider for use DB based on domain request. Here middlewares are called after service providers so I think this is a good way to configure database here and that's why I use service provider.

public function register() {
	//Select DB based on domain url...
	$currDomain = url();
	$currDB = 'mysql1';
	if($currDomain == 'www.d2.com') $currDB = 'mysql2';

	config([
		'database.default' => $currDB,
	]);
	DB::reconnect(); // this will help in reconnecting the database if the connection is already established. and import the DB facade. 
}

There is a more way to this but based on my knowledge this is good. Hope this help you!

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

erdely erdely Joined 7 Nov 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.