Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel collection chunk() method

I’m building an application on Laravel where I’m trying to store 100 datasets into DynamoDB, after reading the documentation DynamoDB can only accept 25 records at a time so I’m trying to store this in chunks of 25.

For this I tried to use Laravel’s collection method, but somehow the format is getting changed and I’m getting DynamoDB error SerializationException, here’s my code:

$posts = collect($request->posts)->map(function ($items){
  //... transforming the array and returning it
})->chunk(25)

foreach($posts->all() as $chunk) {         
    Post::batchPutItem(collect($chunk)->toArray());
}


return response()->json(['data' => $posts], 200);

First 25 records are saved appropriately, I start getting errors from second iteration. When I inspected the data/returned the chunk I found that data gets transformed

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

returned results

As you can see the first chunk is in array, but next chunk becomes object.

I even tried ->chunk(25, true) but no luck.

Any solution or work around to this?

>Solution :

Linked in the comments show the underlying reason. JS handles associative arrays in PHP by converting them to objects, with each index used as an object key. To get around this, you’d need to further map the $posts:

return response()->json(['data' => $posts->map(function ($chunk) {
  return $chunk->values();
  /* or `return array_values($chunk)` if `$chunk` is an Array instead of a Collection */
}], 200);

With a simple dataset of:

$posts = collect([0, 1, 2, 3, 4, 5, 6, 7])->chunk(4);

echo json_encode($posts);
echo json_encode($posts->map(function ($chunk) { 
  return $chunk->values();
});

The first will output what you’re seeing:

[[0,1,2,3],{"4":4,"5":5,"6":6,"7":7}]

While the second will output it as you’re expecting:

[[0,1,2,3],[4,5,6,7]]

Links for reference:

https://laravel.com/docs/10.x/collections#method-values

https://php.net/manual/en/function.array-values.php

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading