2

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@JON The Database Connections Docs show that the API works exactly the same. I am using connections in one of my projects which runs on 5.6 and it works just fine, so the problem must be with your code not the Laravel API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.