Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 648
Mohammed Hassar Avatar asked Mar 11 '15 11:03

Mohammed Hassar


People also ask

How do I change the database connection in Laravel runtime?

Forum change db at runtime connections. mysql", [ "host" => "localhost", "driver" => "mysql", "default" => "mysql", "database" => $dati->db, "username" => "root", "password" => "" ]); DB::reconnect('mysql'); but it work only for the controller and model where i call the function.

How do I change the default database in Laravel?

To change its default database type, edit the file config/database. php: Search for 'default' =>env('DB_CONNECTION', 'mysql') Change it to whatever required like 'default' =>env('DB_CONNECTION', 'sqlite')

Which are the setting for database connection in Laravel?

Laravel makes connecting with databases and running queries extremely simple. The database configuration for your application is located at config/database. php . In this file you may define all of your database connections, as well as specify which connection should be used by default.

How can check DB connection in Laravel?

Echo the Laravel database name in Blade/PHP This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database. Checking whether the application is connected to a Laravel database.


1 Answers

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'    => '',         ),     ), 
like image 171
Laurence Avatar answered Sep 26 '22 05:09

Laurence