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

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

It returns the result set in an array. But if we have the same field name for both the parent and relational table, for example "created_date", then it will only return the field value from the relational table and overwrite the field value of the parent table.

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

+3


source to share


1 answer


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()

      



+1


source







All Articles