1

I have issue with my query. My sample query is as below

$answers = DB::table('IC2017_AY')
        ->join('IC2017', 'IC2017_AY.UNITID', '=', 'IC2017.UNITID')
        ->join('SFA1617_P1', 'IC2017_AY.UNITID', '=', 'SFA1617_P1.UNITID')
        ->join('cost', 'IC2017_AY.UNITID', '=', 'cost.UNITID')
        ->join('gpa', 'IC2017_AY.UNITID', '=', 'gpa.UNITID')
        ->join('enrollment', 'IC2017_AY.UNITID', '=', 'enrollment.UNITID')
        ->join('major', 'IC2017_AY.UNITID', '=', 'major.UNITID')
        ->join('preference', 'IC2017_AY.UNITID', '=', 'preference.UNITID')
        ->select('IC2017_AY.TUITION2 as in_state_tuition','IC2017_AY.FEE2 as in_state_fee','IC2017_AY.TUITION3 as out_state_tuition','IC2017_AY.FEE3 as out_state_fee','IC2017_AY.CHG4AY3 as books_supplies','IC2017_AY.CHG6AY3 as other_expenses','IC2017.ROOMAMT as room_charge','IC2017.BOARDAMT as board_charge','IC2017.RMBRDAMT as combined_charge','SFA1617_P1.SCUGRAD as total_receiving','SFA1617_P1.SCUGFFN as full_time_receiving','SFA1617_P1.UAGRNTN as total_receiving_grant')
        ->where($where_data);
        ->get();

but now I want to pass the array of join because joins will be decided at run time something like this below code is just the example.

$joins = [
['country', 'user_master.country_id', '=', 'country.country_id'],
['city', 'user_master.city_id', '=', 'city.city_id'],
['login_master', 'user_master.user_id', '=', 
'login_master.user_id'],
];

How can I do this in laravel 5.8 thanks in advance.!

1 Answer 1

1

You can use foreach loop for example:

$joins = [
['country', 'user_master.country_id', '=', 'country.country_id'],
['city', 'user_master.city_id', '=', 'city.city_id'],
['login_master', 'user_master.user_id', '=', 
'login_master.user_id'],
];

$answers = DB::table('IC2017_AY')
        ->join('IC2017', 'IC2017_AY.UNITID', '=', 'IC2017.UNITID')
        ->join('SFA1617_P1', 'IC2017_AY.UNITID', '=', 'SFA1617_P1.UNITID')
        ->join('cost', 'IC2017_AY.UNITID', '=', 'cost.UNITID')
        ->join('gpa', 'IC2017_AY.UNITID', '=', 'gpa.UNITID')
        ->join('enrollment', 'IC2017_AY.UNITID', '=', 'enrollment.UNITID')
        ->join('major', 'IC2017_AY.UNITID', '=', 'major.UNITID')
        ->join('preference', 'IC2017_AY.UNITID', '=', 'preference.UNITID');

foreach($joins as $key=>$value){
$answer = $answer->join(implode(",",$value));
}

return $answer->select('IC2017_AY.TUITION2 as in_state_tuition','IC2017_AY.FEE2 as in_state_fee','IC2017_AY.TUITION3 as out_state_tuition','IC2017_AY.FEE3 as out_state_fee','IC2017_AY.CHG4AY3 as books_supplies','IC2017_AY.CHG6AY3 as other_expenses','IC2017.ROOMAMT as room_charge','IC2017.BOARDAMT as board_charge','IC2017.RMBRDAMT as combined_charge','SFA1617_P1.SCUGRAD as total_receiving','SFA1617_P1.SCUGFFN as full_time_receiving','SFA1617_P1.UAGRNTN as total_receiving_grant')
        ->where($where_data);
        ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.