Laravel 4: Joining two tables using Eloquent for multiple database connections -


i facing problem joining 2 tables using eloquent multiple database connections(example main database,db1 ,db2,db3 , on). let me explain in brief below:-

suppose, have 2 tables 1. categories , 2. products. models both tables below:-

1) category.php

class category extends eloquent{

public $timestamps = false;  protected $table = 'categories; protected $fillable = array('v_name','e_status');  public function products() {     return $this->belongsto(product,'i_category_id'); } 

}

2) product.php

class product extends eloquent{

public $timestamps = false;  protected $table = products; protected $fillable = array('v_name',’i_category_id’,'e_status');  public function categories() {     return $this->belongsto(category,'i_category_id'); } 

}

now, in productcontroller.php

$objproduct = new product;  $objproduct->setdbconnection($objdataauth->v_db_name);  // dynamic database connection name $arrproductdetail = $objproduct->get()->section_activities()->get(); 

$arrproductdetail not retrieve information related category(i.e of dynamic database). but, retrieves category of main database(i.e in app/database.php). if take $objproduct->get() retrieves product of new database(db1, db2....) after rnd found eloquent orm uses multiple select queries rather join.

what our concept have 1 main database , other dynamic database created system. need connect multiple database tables functionality, cannot right now. cannot override main database join new dynamic one.

can have solution of this? laravel provide functionality close/destroy previous connection , connect new db(like core php)?

thanks

please add below code in app/database.php

'mysql_dynamic_connection' => array(             'driver'    => 'mysql',             'host'      => 'host',             'database'  => 'db_name',             'username'  => 'username',             'password'  => 'password',             'charset'   => 'utf8',             'collation' => 'utf8_unicode_ci',             'prefix'    => '',         ), 

now, add below line in both model product , category,

protected $connection = 'mysql_dynamic_connection'; 

now, set use database connection in controller file as

config::set('database.connections.mysql_dynamic_connection.database', $objdataauth->v_db_name); 

where mysql_dynamic_connection = database connection in app/database.php , $objdataauth->v_db_name = database connection name

$objproduct = new product;  $arrproductdetail = $objproduct->where('id','=',1)->first()->categories()->get(); echo '<pre>'; print_r($arrproductdetail); 

you category array of product id 1.

thanks, monang shah


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -