@@ -48,16 +48,16 @@ export class Converter
4848 // left_sql currently may start with "DB::table('...')" or "->from('...')" when nested; ensure proper prefix
4949 let base = ( new Converter ( left . Query || left , this ) . run ( false ) ) ;
5050
51- // Build union call: pass the right query builder expression as a DB::raw of the SQL from right_converter without trailing get();
52- // Prefer passing DB::table(...) closure to preserve query builder: use function($query) { $query->from(...)->... }
53- let right_closure = 'function ($query) {\n\t' + right_sql . replace ( 'DB::table' , '$query->from' ) . split ( '\n' ) . join ( '\n\t' ) + ';\n}' ;
51+ // Build union call: pass the right query builder expression directly (as DB::table(...)->...)
52+ let right_block = right_sql ;
5453
55- let union_section = base + '\n->' + union_method + '(' + right_closure + ')' ;
54+ // Indent right block and wrap in parentheses as argument to union/unionAll
55+ let union_section = base + '\n->' + union_method + '(\n' + addTabToEveryLine ( right_block , 1 ) + '\n)' ;
5656
5757 // If top-level has order_by / limit / offset, append them
5858 if ( propertyExistsInObjectAndNotNull ( this . ast , 'order_by' ) && this . ast . order_by . length > 0 ) {
5959 union_section = union_section + '\n->' + ( new Converter ( this . ast , this ) . resolveOrderBySection ( ) ) ;
60- } else if ( propertyExistsInObjectAndNotNull ( this . ast , 'order_by' ) && this . ast . order_by . length === 0 && propertyExistsInObjectAndNotNull ( this . ast . body , 'order_by' ) && this . ast . body . order_by . length > 0 ) {
60+ } else if ( propertyExistsInObjectAndNotNull ( this . ast . body , 'order_by' ) && this . ast . body . order_by . length > 0 ) {
6161 union_section = union_section + '\n->' + ( new Converter ( this . ast . body , this ) . resolveOrderBySection ( ) ) ;
6262 }
6363
0 commit comments