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.
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.
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')
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.
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.
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' => '', ), ),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With