0

I am trying to get following query on Laravel:

SELECT 
  table1.* , 
  table1.colb as aaa
FROM 
  table1 
  LEFT jOIN table2 on table2.cola = table1.colb

This is the Laravel DB Query code :

DB::table('table1')
        ->leftJoin('table2','table2.cola', '=', 'table1.colb')
        ->select('table1.* , table1.colb as aaa')
        ->get();

But it doesn't work and I get SQL Syntax error. This is the SQL which Laravel is making using above code which is wrong :

select `table1`.* as `table1.colb` 
from `table1` 
left join `table2` on `table2`.`cola` = `table1`.`colb`

How can I fix this using laravel way?

1 Answer 1

1

You need to pass multiple parameters to the select method for each one to get prepared correctly:

DB::table('TABLE1')
        ->leftJoin('TABLE2','TABLE2.COLA', '=', 'TABLE1.COLB')
        ->select('TABLE1.*', 'TABLE1.COLB AS AAA') // <- 2 separate params here
        ->get();
Sign up to request clarification or add additional context in comments.

Comments

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.