User::chunk(100, function($uyeler)
{
foreach ($uyeler as $uye)
{
echo $uye->username.'<br>';
}
});
Şeklinde yapacağın çağrı, verdiğin sayı kadar sayfalama yapar sonucu callback olarak verdiğin fonksiyona gönderir ve tekrar sayfalamaya devam eder.
Mesela tablo da 1000 kullanıcı olsaydı, yukarıda yazdığım kod çalıştığında callback olarak tanımladığım
function($uyeler)
{
foreach ($uyeler as $uye)
{
echo $uye->username.'<br>';
}
}
fonksiyonuna kullanıcılar 100,100 gelecek ve ben 1000 kullanıcının tamamını işlemiş olacaktım.
chunk metodu aşağıdaki gibidir.
public function chunk($count, $callback)
{
$results = $this->forPage($page = 1, $count)->get();
while (count($results) > 0)
{
// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
call_user_func($callback, $results);
$page++;
$results = $this->forPage($page, $count)->get();
}
}