Laravel 5 如何检查最后一块?

Laravel 5 如何检查最后一块?,laravel-5,eloquent,Laravel 5,Eloquent,我使用chunk方法chunk来获取大数据,但我需要知道最后一个chunk,因为我将数据放入文件中,在最后一个chunk,我不想向文件中添加任何内容,所以我需要知道最后一个chunk DB::table('users')->chunk(100, function($users) { //how to know if it's the last chunk or not ? foreach ($users as $user) { // } })

我使用chunk方法chunk来获取大数据,但我需要知道最后一个chunk,因为我将数据放入文件中,在最后一个chunk,我不想向文件中添加任何内容,所以我需要知道最后一个chunk

DB::table('users')->chunk(100, function($users)
{
    //how to know if it's the last chunk or not ?
    foreach ($users as $user)
    {
        //
    }
});

它们是了解最后一个块的一种方法吗?您可以在laravel中看到关于块的文档,您可以通过如下计数器手动完成:

//get total number of users
$noOfUsers = DB::table('users')->count();

$chunk = 100;

$iterations = ceil($noOfUsers/$chunk);//this many times the loop should go on

//start a counter with 1 and increase it every time
$counter = 1;

DB::table('users')->chunk($chunk, function($users) use (&$counter, $iterations)
{
    //when counter gets equal to iterations, thats your last chunk
    if ($counter === $iterations) {
       //this is the last chunk
    }

    foreach ($users as $user) {
       //
    }
    $counter++;
});

谢谢你的解决方案,但我一直在寻找一些受欺负的方法来做到这一点,我怀疑是否有一个