错误 - 无法遍历已关闭的生成器

I ran into this error today and sort of lost as to how to deal with it.

My app is grabbing data from an API and with that data, I query my database, get required data, create new value and send it back to API to update.

I have done some googling but still getting the hang of MVC and laravel, nothing I have read I could get to work with my code:

// Create Connection
$client = new Name\App(
      env('DOMAIN'),
      env('API_KEY'),
      env('PASSWORD'),
      env('SECRET')
    );

    //Get data from API
    $something = $client->getSomething('something');

    // Make empty Array
    $arr = [];

    // loop through data from API and create array of required data
    foreach ($something as $thing) {
      $arr[] = array('colors' => $thing->color);
    }

    // query database for values found from API array
    $eg = DB::table('table')
    ->select('eg','size', 'weight')
    ->whereIn('eg', $arr) // whereIn to query array
    ->get();

    // create another blank array
    $data = [];

    // loop the above query 
    foreach ($eg as $type) {

      // create json value to send back to api 
      // and update
      $data[] = [
        'mindfull' => [
          'this' =>  $type->en,
          'that' =>  $type->tva
        ]
      ];
    }

    // update API (this gives me the traverse generator error)
    foreach ($something as $again) {
      $client->put('link/' . $again->id, $data);
    }
  }

Any Ideas would be helpful.

This adds another iteration to your algorithm, but I guess this has to work:

Replace

//Get data from API
$something = $client->getSomething('something');

with

//Get data from API
$something = []
foreach ($client->getSomething('something') as $thing)
   $something []= $thing;

This way, the original generator is iterated only once. After that you have your array with the data.