3

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

1

1 Answer 1

1
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()
6
  • It is working but if i have field like "Fee" with float value and field type (8,2) on both parent and relational table then it will returning wrong value. for example, parent table "Fee" field has value "111.50" and relational field has "11.00", but it was returning parent field value as "110" instead of "111.50", Why..? Jan 2, 2015 at 10:15
  • So, you can make another alias, mysql want exactly know what he should get.
    – xAoc
    Jan 2, 2015 at 10:20
  • I have already created alias for "Fee" field as below: // QUERY USING LEFT JOIN with Eloquent $queryObj = Appraisal::leftJoin('scope_appraisal_info', function ($join) {$join->on('scope_appraisal.appraisal_id', '=', 'scope_appraisal_info.appraisal_id');})->selectRaw('scope_appraisal.appraisal_id,scope_appraisal.appraisal_info_fee as main_fee,scope_appraisal_info.appraisal_info_fee as sub_fee')->where('scope_appraisal.appraisal_assignedto_appraisal_id', '=', $appraiserId); $result = $queryObj->get(); print_r($result); Jan 2, 2015 at 10:23
  • Received response as below: [items:protected] => Array ( [0] => V2_0\Appraisal Object ( [table:protected] => scope_appraisal [primaryKey:protected] => appraisal_id [attributes:protected] => Array ( [appraisal_id] => 9427 [main_fee] => 110 [sub_fee] => 0 ) Jan 2, 2015 at 10:27
  • And are you sure that there is not 0? Query looks correct
    – xAoc
    Jan 2, 2015 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.