Problem return in function laravel 5.5

0

until now I have managed to count the children of each parent, that is to say, with the following method I achieve the following result.

  public function reportes(Request $request)
    { 
        $request->user()->authorizeRoles(['admin']);

        $reporuser=usuarios::distinct('usuario_ad')->pluck('usuario_ad');


          foreach($reporuser as $reporuser)

          {

            $conteo=usuarios::where('usuario_ad',$reporuser)->count();
            echo $conteo


          }

        
     }

the result is 4233, so a parent user has 4 children, another 2, and the last two parents have 3 users, the expected result, but when I replace echo by return it only shows me the first value, I mean 4 , and I do not know why.

    
asked by zereft 15.12.2018 в 23:46
source

1 answer

0

The problem is that when doing a return you are indicating that you leave the function immediately, returning the value you indicate. That is, you enter the for the first time and tell him I left the function and returns $ count that at that time has only the value 4. In order for it to work as you intended, you should return the value after the loop. Greetings!

Something like this:

public function reportes(Request $request)
{ 
    $request->user()->authorizeRoles(['admin']);

    $reporuser=usuarios::distinct('usuario_ad')->pluck('usuario_ad');

     $conteo=0;
      foreach($reporuser as $reporuser)
      {
        $conteo= $conteo + usuarios::where('usuario_ad',$reporuser)->count();
      }

    return $conteo;
 }
    
answered by 15.12.2018 / 23:55
source