-1

In the Below Code i was using multiple table joins and i have multiple rows with the same id from the table clients_topup table while i need to count multiple rows from the table and display all data from the other two tables

in the below code i was getting only count values how to make it out

foreach ($clients as $client) {
            $clientsLevelOne = DB::table('clients')
            ->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
            ->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
            ->select(DB::raw("SUM(topup_amount) as count"))
            ->where('clients.under_reference','=',$client->reference_id)->get();        

            $users[] = $clientsLevelOne;
          }

1 Answers1

0

i think you to do like this:

  $client_id_list =  [];
  foreach ($clients as $client) {
      $client_id_list[] => $client->id,
  }

 $clientsLevelOne = DB::table('clients')
            ->select(DB::raw("SUM({table_name}.topup_amount) as count"))
            ->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
            ->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
            ->where('clients.under_reference','=',$client->reference_id)
            ->whereIn('client_id', $client_id_list)
            ->groupBy('client_id')
            ->get();
LuongPs
  • 99
  • 6