Reputation: 899
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.!
Upvotes: 1
Views: 136
Reputation: 880
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();
Upvotes: 1