How to use multiple databases in Laravel

前端 未结 4 1534
清歌不尽
清歌不尽 2020-11-21 23:47

I want to combine multiple databases in my system. Most of the time the database is MySQL; but it may differ in future i.e. Admin can generate such a reports which is

4条回答
  •  余生分开走
    2020-11-22 00:13

    Actually, DB::connection('name')->select(..) doesnt work for me, because 'name' has to be in double quotes: "name"

    Still, the select query is executed on my default connection. Still trying to figure out, how to convince Laravel to work the way it is intended: change the connection.

    Edit: I figured it out. After debugging Laravels DatabaseManager it turned out my database.php (config file) (inside $this->app) was wrong. In the section "connections" I had stuff like "database" with values of the one i copied it from. In clear terms, instead of

    env('DB_DATABASE', 'name')
    

    I needed to place something like

    'myNewName'
    

    since all connections were listed with the same values for the database, username, password, etc. which of course makes little sense if I want to access at least another database name

    Therefore, every time I wanted to select something from another database I always ended up in my default database

提交回复
热议问题