How can I handle subdomains with one laravel installation

前端 未结 7 2028
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 09:19

I am creating a laravel project for which I need one laravel installation and use its instance in sub-domain with separate database. And th

7条回答
  •  遥遥无期
    2020-12-29 10:06

    Laravel supports multiple Database connections. Firstly, define the connections in config/database.php:

     'default_connection',
    
        'connections' => array(
    
            // domain.com
            'default_connection' => array(
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'primary_database',
                'username'  => 'username',
                'password'  => 'password'
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
    
            // sub.domain.com
            'subdomain_connection' => array(
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'secondary_database',
                'username'  => 'username',
                'password'  => 'password'
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
        ),
    );
    

    Now to specify which connection your models should use you can set the $connection property in your models:

    You can set the value of $connection programatically.

提交回复
热议问题