Model Based Setting Different Connection

Submitted by dhrubo001 - 8 years ago

Model Based Setting Different Connection when working with 2 or more database in laravel

1. First Create You Connection in database.php

'mysql' => array(
    		'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'superadmin',
			'username'  => 'root',
			'password'  => 'knwall',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => '',
		),
		
		'mysql2' => array(
			'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'framework',
			'username'  => 'root',
			'password'  => 'knwall',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => '',
		),
        
2.Now just specify which connection to use in models default will use the "mysql" connection

A)Organization.php
class Organization extends Eloquent{
    //// will use "mysql
    
}

B)class FrameworkOrganization extends Eloquent{
  protected $connection = 'mysql2'; /// Other connection
}