tajihiro
tajihiro

Reputation: 2443

How to give alias name on same name columns

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

Answers (1)

Hadi Ahmadi
Hadi Ahmadi

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

Related Questions