Mohsen Saberi
Mohsen Saberi

Reputation: 19

Laravel eloquent chunk method don't return all records

In the following snippet the chunk method is called just over the half of the records in which api_phonebook_option_id is 0.

    $counter = 0;
    $numberOfOptions = $this->oldOptionModel->where('api_phonebook_option_id', 0)->count();

    $this->oldOptionModel->where('api_phonebook_option_id', 0)->orderBy('phonebook_option_id', 'desc')
        ->chunk(50, function (Collection $options) use (&$counter, $numberOfOptions) {

            // do something
            // which don't have return false statement
            $counter += 1;
        });
    echo $counter;

Any comments and suggestions are appreciated

Edit:

    echo ($counter * 50 == $numberOfOptions); // This line prints false

Upvotes: 0

Views: 568

Answers (1)

Zafar Ahmed
Zafar Ahmed

Reputation: 359

$this->oldOptionModel->where('api_phonebook_option_id', 0)->orderBy('phonebook_option_id', 'desc')
        ->chunk(50, function (Collection $options) use (&$counter, $numberOfOptions) {
            foreach($numberOfOptions as $numberOfOption){
              // do something
              // which don't have return false statement
              $counter += 1;
            }
        });

Do foreach in function to get single data item

Upvotes: 1

Related Questions