Laravel:我想返回块函数的结果,用Ajax在视图中获取它

I want to return the result of chunk. The problem is , when I iterate with foreach, I put echo result it displays the result but when I want to return it , I have a blank page

$tab = array();

Product::blabla ->chunk (500, function($results))

{


  foreach($results as $result)

  {
    array_push ($tab,$result);
    echo $results;// works
    return $results;// doesn't return anything

   }

}

return $tab; // to be sent to Ajax type get

Closure can be stored into variable, so just add to variable.

$tab = Product::blabla()->chunk (500, function($results))
{
    // your logic
    return $results;
};

return $tab

This is not working actually, it just return true. You have to make the closure function use an external variable by reference example:

$output = [];

$tab = Product::blabla()->chunk (500, function($results) use (&$output))
{
    $output = array_combine($output, $results);
};

return $output;