Laravel DB Fasade for Join query with a union()

423 views Asked by At

When I have set up a ".env" for using two DBs, also made code as below for use it.

but where() method was incorrect to use it.

Could you tell me more detailed usages to use where() method with explanation or tell some link to study?

Thanks in advenced.

$master = DB::connection('master_db')->table('customer_master')
            ->where(['name', '=', 'string'],
                    ['tel', '=', 'integer'],
                    ['address','=', 'string']);

$slave = DB::connection('slave_db')->table('customer_slave')
            ->where(['histories', '=', 'string'])
            ->union($master)
            ->get();
1

There are 1 answers

0
Harry On

Write the query like this:

$master = DB::connection('master_db')->table('customer_master')
    ->where([
        'name' => 'string',
        'tel' => 'integer',
        'address' => 'string'
    ]);

$slave = DB::connection('slave_db')->table('customer_slave')
    ->where('histories', 'string')
    ->union($master)
    ->get();

Here the array syntax has been tweaked for $master and where() tweaked for $slave. The = comparison is the default so no need to specify here.