change database connection in laravel model
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' => '',
),
),
Laravel, change connection in model for one method?
Eloquent provides a nice way to handle multiple connections.
You should just be able to use the on
method. For example.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TableName extends Model
{
protected $table = 'table_name';
protected $connection = 'dev';
public $timestamps = false;
public static function live($index) {
$liveId = Settings::where('index', $index)->get()[0];
$data = self::on('live')->where('index', $liveId->live_index)->get();
return $data;
}
}
That should then run the query using the live
connection in your database configuration.
Laravel 8 Change database connection globally in the runtime
I found the working solution.
$readOnlyConfig = config('database.connections.mysql_readonly');
Config::set('database.connections.mysql', $readOnlyConfig);
DB::purge();
Laravel 8 - Changing database connection for model
use on if you are changing for one query.
Company::on('mysql_prod')->where('symbol', $symbol)->first();
define $connection
property in Company
model so no need to mention connection
protected $connection = 'mysql_prod';
so query is
Company::where('symbol', $symbol)->first();
Ref:https://laravel.com/docs/8.x/eloquent#database-connections
Laravel / Eloquent : change connection and get all
All()
is a static function.
In this case, use get()
:
$persons = BoPerson::on('sqlite')->get();
Source: http://laravel.com/docs/4.2/eloquent#basic-usage
Change the Database Connection Dynamically in Laravel
Well you can use the default database for user login and have a new field for the database name. Then whenever you need to query a different database, you can just change your db connection.
Something like this
$someModel = new SomeModel;
$databaseName = "mysql2"; // Dynamically get this value from db
$someModel->setConnection($databaseName);
$something = $someModel->find(1);
You can read more about it here.
http://fideloper.com/laravel-multiple-database-connections
Laravel 5 How to change a model database connection within controller
You can switch the connection using the on()
static method:
YourModel::on('connection_name')->get();
Source
How to change Eloquent Builder's connection?
Need to get the Query from Eloquent Builder and set a new connection to that and set the new query to the Eloquent Builder as below:
$builder = User::query();
$conn = DB::connection('mysql_server2');
$builder->where('age', '>', 20);
$builder->with('orders');
// Magic Happens Here
// Change Connection Here
$query = $builder->getQuery();
$query->connection = $conn;
$query->grammar = $conn->query()->getGrammar();
$query->processor = $conn->query()->getProcessor();
$builder = $builder->setQuery($query);
Related Topics
Does PHP's $_Request Method Have a Security Problem
Preg_Replace with Array Replacements
Large Hex Values with PHP Hexdec
Is It Really That Wrong Not Using Setters and Getters
Pdo Catch and Output MySQL Errors
How to Remove Values from Two Arrays That Have the Same Key and Value
PHP E-Mail Form Sender Name Instead of E-Mail
Using '$This' in an Anonymous Function in PHP Pre 5.4.0
Ios7 - Receipts Not Validating at Sandbox - Error 21002 (Java.Lang.Illegalargumentexception)
Php: Fopen() Permission Denied
PHP Password_Hash and Password_Verify Issues No Match
How to Swap Colors in Image Using Gd Library in PHP
How to Use Prepared Statements with Postgres
Differencebetween Get_Result() and Store_Result() in PHP
How to Capture Search Term from Google Search