db->table("bds_installment_payment_details bipd") ->select("bipd.*, ct.client_name,ct.short_name, cb.branch_name, leads.salse_person_id,cp.policy_no") ->join("policy_transaction pt", "pt.id = bipd.pt_id") ->join("clients ct", "ct.id = pt.client_id") ->join("client_branch cb", "cb.id = pt.client_branch_id") ->join("leads", "leads.id = bipd.lead_id") ->join("client_policy cp","cp.id = pt.client_policy_id") ->where("bipd.is_active", 1) ->where("bipd.payment_date", date('Y-m-d', strtotime('+15 days'))); $data = $builder->get()->getResultArray(); // Loop through to extract sales person details foreach ($data as &$row) { $sales_person_ids = json_decode($row['salse_person_id'], true); $sales_person_id = $sales_person_ids[0] ?? null; if ($sales_person_id) { $user = $this->db->table('user_profiles') ->select('email') // or whatever field ->where('id', $sales_person_id) ->get() ->getRowArray(); $row['sales_person'] = $user['email'] ?? 'N/A'; } else { $row['sales_person'] = 'Not Assigned'; } $head = $this->db->table('user_profiles') ->select('email') // or whatever field ->where('role', 5) ->where('is_active',1) ->get() ->getResultArray(); $row['heads'] = $head; $admin = $this->db->table('user_profiles') ->select('email') ->where("is_active",1) ->where("role",1) ->get() ->getResultArray(); $row['admins'] = $admin; $buisenessTeam = $this->db->table("user_teams ut") ->select("up.email") ->join('user_profiles up','up.id = ut.user_id and up.is_active = 1') ->where("ut.team_id",7) ->where("ut.is_active",1) ->get() ->getResultArray(); $row['buisness_team'] = $buisenessTeam; } return $data; } }