Mohammed Hassar
Mohammed Hassar

Reputation: 553

change database connection in laravel model

So i work with Laravel 4.2, what i want is in one of my models use an external database, this is my model code :

<?php
class McibModel extends Eloquent {
    /**
      * The database table used by the model.
      *
      * @var string
      */
    //here i should call the external database table
    protected $table = 'agencies';
}

So please if someone has any idea i will be very appreciative.

Upvotes: 46

Views: 86807

Answers (5)

Alan
Alan

Reputation: 2086

I needed to do this as well for just one table, and here are the quickest ways I found for Laravel 8:

// config/database.php
'mysqllogs' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_LOGS_HOST', '127.0.0.1'),
    'port' => env('DB_LOGS_PORT', '3306'),
    'database' => env('DB_LOGS_DATABASE', 'forge'),
    'username' => env('DB_LOGS_USERNAME', 'forge'),
    'password' => env('DB_LOGS_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_general_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

To access the new database:

Method 1:
DB::connection('mysqllogs')->statement('DELETE FROM logs where office_id=1');

Method 2:
$log = new Log();
.....
$log->setConnection('mysqllogs');
$log->save();

Method 3:
$logs = Log::on('mysqllogs')->where('office_id', 1);

Upvotes: 0

sudin
sudin

Reputation: 332

You can do it like this to set another connection :

$product= new Product();
$product->setConnection('another_connection');

Upvotes: 0

Symphony0084
Symphony0084

Reputation: 1435

I think for a lot of use cases it can be convenient to declare the connection at runtime like this:

$McibModel = new McibModel();
$McibModel->setConnection('second_db_connection');

Upvotes: 13

Jed Lynch
Jed Lynch

Reputation: 2166

In one way setting a protected property will always connect a particular model to a database.

class MyModel extends Model {

     protected $connection = "second_db_connection";
}

In a query I like this approach...

(new MyModel())->on('second_db_connnection')->get();

Upvotes: 20

Laurence
Laurence

Reputation: 60058

Different models can have different database connections. So your models use the normal default connection - but your 'McibModel' model can use another connection:

class McibModel extends Model {

    protected $connection= 'second_db_connection';

    protected $table = 'agencies';

}

Then in your DB connection file - you would have something like this:

return array(
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'second_db_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),

Upvotes: 83

Related Questions