Union queries from different databases in Laravel Query Builder

后端 未结 2 1497
感动是毒
感动是毒 2021-01-03 14:33

I have two similar tables in two different databases. Both tables have a column with a date and one with email addresses. Though the column names are not the same. As resul

2条回答
  •  无人及你
    2021-01-03 15:22

    You cannot do a UNION query across connections. You'll have to do it as two separate queries:

    $emails1 = DB::connection('db1')->table('contacts_1')
                 ->selectRaw('mail_address as email, date as created_at')->get();
    
    $emails2 = DB::connection('db2')->table('contacts_2')
                 ->select('email', 'created_at')->get();
    
    $emails = array_merge($emails1, $emails2);
    

提交回复
热议问题