3

Is there anyway in the Laravel Eloquent to use two different connections, for inserting, updating and Selecting.

What i am trying to do is specify a connection when user is pulling the data from db, and another one while inserting or updating data.

I am wondering if it can be done with the Eloquent instead of defining connections everytime?

  • possible duplicate of [Laravel 4 - Connect to other database](http://stackoverflow.com/questions/17410049/laravel-4-connect-to-other-database) – Seth Malaki Dec 13 '13 at 09:32
  • 1
    AFAIK the `on` method reported in the documentation is the only provided way (quite good IMO). – matpop Dec 13 '13 at 09:34
  • 1
    See [Implement automated read/write connections in Eloquent](https://github.com/laravel/framework/issues/5) – Holger Weis Dec 13 '13 at 10:28
  • 1
    This feature has recently been added but isn't documented yet. You can check the corresponding [commit](https://github.com/laravel/framework/commit/a3b0e87ae50b22a517c6daea85e824ae2f0e27c0) on how to use it. – Holger Weis Dec 13 '13 at 10:49
  • @HolgerWeis Thanks, really good to know! So the answer is yes, with version 4.1 – matpop Dec 13 '13 at 11:36
  • This feature has now been documented, see my answer. – Holger Weis Dec 14 '13 at 17:34

1 Answers1

4

This is possible with Laravel 4.1. You can configure it in your app/config/database.php like so:

'mysql' => array(
    'read' => array(
        'host' => '192.168.1.1',
    ),
    'write' => array(
        'host' => '196.168.1.2'
    ),
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

See the Read / Write Connections section in the Laravel database documentation.

Holger Weis
  • 1,685
  • 1
  • 13
  • 17