Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

php - 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?

question from:https://stackoverflow.com/questions/65877740/how-to-give-alias-name-on-same-name-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...