Reputation: 2443
I'm using eloquent. I would like to write following code in case of SQL.
SELECT A.col as a_col, B.col as b_col
FROM a_tbl A
JOIN b_tbl B ON A.id = B.id
a_tbl and b_tbl have same name columns.
How can I write this with eloquent?
Upvotes: 0
Views: 77
Reputation: 579
You can do it in this way:
$records = DB::table('a_tbl')
->join('b_tbl', 'a_tbl.id', '=', 'b_tbl.id')
->select('a_tbl.col as a_col', 'b_tbl.col as b_col')
->get();
Also you can read about laravel docs for this: https://laravel.com/docs/8.x/queries#joins
Upvotes: 2