Ad

How To Specify Database Connection Using Statement Method In Laravel

- 1 answer

I am running a raw query on Laravel using DB::statement() method. This statement needs to fetch data from my secondary database.

I wonder if there is any clean way to specify database connection to this method instead of using database name prefix before all fields.

Ad

Answer

Yes, you can use the connection method, which accepts any connection name stored in the config/database.php file:

DB::connection('pgsql')->statement('your statement here...');

You can read more in the Using Multiple Database Connections Documentation.

Ad
source: stackoverflow.com
Ad