1

I have a query that takes id_user from several transactions.

I just want to find the id_user in the user table by taking only 1 of each data.

example: in the query there is a user id [1,1,3,4]

i just want to take [1,3,4]

so the name that comes out is only from id_user [1,3,4] not [1,1,3,4]

I have tried using unique and distinct features but instead the data is repeated several times

this my code :

$arrayDiff = [];
        $totalMasing = [];
        $listuser = [];

        if ($totalSubmitted == 1) {
            for ($i = 0; $i < $totalSubmitted; $i++) {
                $item = $subset[$i]['id_okr'];
                $arrayDiff[] = Okr::find($item)->get();
                $totalMasing[] = Interact::where('id_created', '=', $arrayDiff[0][$i]['id_created'])
                    ->where('status', '=', '2')
                    ->where('id_user', '=', $interact)
                    ->count();

                $listuser[] = User::find($arrayDiff[0][$i]['id_created']);
            }
        }else{
            for ($i = 1; $i < $totalSubmitted; $i++) {
                $item = $subset[$i]['id_okr'];
                $arrayDiff[] = Okr::find($item)->get();
                $totalMasing[] = Interact::where('id_created', '=', $arrayDiff[0][$i]['id_created'])
                    ->where('status', '=', '2')
                    ->where('id_user', '=', $interact)
                    ->count();

                $listuser[] = User::find($arrayDiff[0][$i]['id_created']->distinct('id_created'));

            }

this is my JSON Result :

listuser: [
            {},
            {},
            {}
       ]

any advice for my problem ? thanks so much

sindoro
  • 11
  • 1

1 Answers1

0

I have re written your code with change & comments .

$arrayDiff = [];
    $totalMasing = [];
    $listuser = [];
    $listuserids = [];
    if ($totalSubmitted == 1) {
      for ($i = 0; $i < $totalSubmitted; $i++) {
        $item = $subset[$i]['id_okr'];
        $arrayDiff[] = Okr::find($item)->get();
        $totalMasing[] = Interact::where('id_created', '=', $arrayDiff[0][$i]['id_created'])->where('status', '=', '2')->where('id_user', '=', $interact)->count();
        $listuserids[] = $arrayDiff[0][$i]['id_created']; // add this line
        //$listuser[] = User::find($arrayDiff[0][$i]['id_created']); // remove this line
      }
    } else {
      for ($i = 1; $i < $totalSubmitted; $i++) {
        $item = $subset[$i]['id_okr'];
        $arrayDiff[] = Okr::find($item)->get();
        $totalMasing[] = Interact::where('id_created', '=', $arrayDiff[0][$i]['id_created'])->where('status', '=', '2')->where('id_user', '=', $interact)->count();
        $listuserids[] = $arrayDiff[0][$i]['id_created']; // add this line
        //$listuser[] = User::find($arrayDiff[0][$i]['id_created'])->distinct('id_created')); // remove this line
      }
    }
    // and place query outside of loop!
    $listuser[] = User::whereIn('id', $listuserids)->get();

This solution will solve your problem and return you only unique result. & it will also improve performance of your code.

(Important Note: Try to not put a query in loop.)

Vishal Tarkar
  • 808
  • 11
  • 32