47

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.

5 Answers 5

85

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'    => '',
        ),
    ),
Sign up to request clarification or add additional context in comments.

2 Comments

Also note the on() method which let's you specify the connection on the fly: McibModel::on('second_db_connection')->get()
Exactly what I was looking for @lukasgeiter
20

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();

Comments

13

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');

Comments

0

You can do it like this to set another connection :

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.