Keyur Vaghani
Keyur Vaghani

Reputation: 206

How to alias columns in Laravel Eloquent ORM using "leftJoin"

I have noticed that if we used Eloquent "LeftJoin" in query,

It is returning result set in array But, If we have same field name on both parent and relational table for example "created_date" then it will only returns field value from relational table and overwrite value of parent table field.

How can we get value for parent table field (create_date) value as well as relational table field (create_date) value..?

Upvotes: 3

Views: 4955

Answers (1)

xAoc
xAoc

Reputation: 3608

MainTable::leftJoin('LeftJoinTable', 'LeftJoinTable.main_id', '=', 'MainTable.id')->
selectRaw("MainTable.create_date as main_create_date, LeftJoinTable.create_date as leftJoin_create_date")->get(); //or ->first()

Upvotes: 1

Related Questions