Laravel uses multiple databases.

Source: Internet
Author: User
In the past few days, Laravel has been used to develop a system. This system connects two databases. A name is blog and a name is center. The center database serves as the user center. Several other systems may be connected to the public database. It is mainly used for user login authentication. Blog data... a system is being developed using Laravel these days. This system connects two databases. A name is blog and a name is center.
The center database serves as the user center. Several other systems may be connected to the public database. It is mainly used for user login authentication.
The function of the blog database is to publish an article, which does not involve authentication.

My idea is to use the center database as the user's login authentication. After logging on, I will send an article to switch to the blog database.

Currently, my.envThe configuration is as follows:

DB_HOST=localhostDB_DATABASE=blogDB_DATABASE_CENTER=centerDB_USERNAME=rootDB_PASSWORD=root

The design in database. php is as follows:

    'mysql' => [        'driver'    => 'mysql',        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE', 'forge'),        'username'  => env('DB_USERNAME', 'forge'),        'password'  => env('DB_PASSWORD', ''),        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',        'strict'    => false,    ],    'mysql_center' => [        'driver'    => 'mysql',        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE_CENTER', 'forge'),        'username'  => env('DB_USERNAME', 'forge'),        'password'  => env('DB_PASSWORD', ''),        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',        'strict'    => false,    ],

My own modelUser.phpYou can switch the database in the following way.

class User extends Model implements AuthenticatableContract, CanResetPasswordContract{    protected $connection = 'mysql_center';

But how does the Controller switch the database in the example provided by the system?

app\Http\Controllers\Auth\AuthController.phpapp\Http\Controllers\Auth\PasswordController.php

I hope you can help me solve this problem. Thank you!

Reply content:

In the past few days, Laravel has been used to develop a system. This system connects two databases. A name is blog and a name is center.
The center database serves as the user center. Several other systems may be connected to the public database. It is mainly used for user login authentication.
The function of the blog database is to publish an article, which does not involve authentication.

My idea is to use the center database as the user's login authentication. After logging on, I will send an article to switch to the blog database.

Currently, my.envThe configuration is as follows:

DB_HOST=localhostDB_DATABASE=blogDB_DATABASE_CENTER=centerDB_USERNAME=rootDB_PASSWORD=root

The design in database. php is as follows:

    'mysql' => [        'driver'    => 'mysql',        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE', 'forge'),        'username'  => env('DB_USERNAME', 'forge'),        'password'  => env('DB_PASSWORD', ''),        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',        'strict'    => false,    ],    'mysql_center' => [        'driver'    => 'mysql',        'host'      => env('DB_HOST', 'localhost'),        'database'  => env('DB_DATABASE_CENTER', 'forge'),        'username'  => env('DB_USERNAME', 'forge'),        'password'  => env('DB_PASSWORD', ''),        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',        'strict'    => false,    ],

My own modelUser.phpYou can switch the database in the following way.

class User extends Model implements AuthenticatableContract, CanResetPasswordContract{    protected $connection = 'mysql_center';

But how does the Controller switch the database in the example provided by the system?

app\Http\Controllers\Auth\AuthController.phpapp\Http\Controllers\Auth\PasswordController.php

I hope you can help me solve this problem. Thank you!

See document: http://laravel.com/docs/master/authentication

AuthController uses the "App \ User" Eloquent model by default. Similarly, you can specify a database in App \ User.

Add the login registration and password retrieval sections in the Auth case provided by laravel 5.
You can useylem.

For password retrieval/config/auth.php.
For example:

'table' => 'mydatabases.password_resets',

'Host' => env ('db _ host', 'localhost'), 'database' => env ('db _ DATABASE_CENTER ', 'forge '), 'username' => env ('db _ username', 'forge'), 'Password' => env ('db _ password ',''), 'charset' => 'utf8', 'colation' => 'utf8 _ unicode_ci ', ========================== incorrect writing ...... Remove env, env ('db _ DATABASE_CENTER ', 'forge'), leaving only 'forge', env ('db _ password ',''), I only left ''/* behind to solve this problem. As a newbie, I spent a total of two days */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.