Alee Waqas Mirza
Alee Waqas Mirza

Reputation: 75

Laravel using different connections for Inserting & Selecting data

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?

Upvotes: 3

Views: 2643

Answers (1)

Holger Weis
Holger Weis

Reputation: 1695

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.

Upvotes: 4

Related Questions