Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel how to use query builder DB::table(..) with DB::connection()

Tags:

php

laravel

I have 2 databases defined in config/databases, mysql and mysql2
The default connection is mysql

I need to get this data from mysql2

$programs=DB::table('node')->where('type', 'Programs')->get();

The docs tell me I can change the connection using

$programs=DB::connection('mysql2')->select(...)

which would let me run a sql statement to get an array for $programs. But I am wondering if there is a way to combine the 2 statements i.e. use query builder on specific db::connection.

like image 283
Phil Avatar asked Oct 03 '14 14:10

Phil


1 Answers

You should use:

$programs=DB::connection('mysql2')->table('node')->where('type', 'Programs')->get();
like image 92
Marcin Nabiałek Avatar answered Oct 24 '22 04:10

Marcin Nabiałek