Laravel Chunk Method Using Eloquent Example

Today, We want to share with you laravel chunk.In this post we will show you laravel order by desc, hear for firstorfail laravel we will give you demo and example for implement.In this post, we will learn about Pluck In Laravel with an example.

Laravel Chunk Eloquent Method Example

The chunk() method is use to split the collection into multiple smaller collection. Its useful into the pagination and listing of items.

$patients = Patient::all();
$charge = $patients->chunk(4);

What is Chunks?

Chunks an array into arrays with length elements.

Laravel comes up with an excellent Eloquent ORM. Chunking the array or collection is the best way to process large data in PHP.

array_chunk ( array $array , int $length , bool $preserve_keys = false ) : array.

pagination to your query like so:

$data = Inspector::latest('id')
    ->select('id', 'pcode', 'status', 'desc', 'price')
    ->where('pcode', 'LIKE', '%' . $searchtext . '%')
    ->paginate();

Process big DB table with chunk() method

$products = Product::all();
foreach ($products as $product) {
  $data_results = ($product->some_field > 0) ? 1 : 0;
  // might be more logic here
  $product->update(['db_clm_key' => $data_results]);
}

Product::chunk(100, function ($products) {
  foreach ($products as $product) {
    $data_results = ($product->some_field > 0) ? 1 : 0;
    // might be more logic here
    $product->update(['db_clm_key' => $data_results]);
  }
});

Use laravel chunk with Eloquent Model

laravel controller

$products = Product::all(); // Fetch some data

Laravel View:

@foreach($products->chunk(3) as $row)
    <div class="grid__row">
        @foreach($row as $product)
            <div class="grid__item">
                {{ $product->name}}
            </div>
        @endforeach
    </div>
@endforeach

all types of laravel collections

$players = collect(['bhavik', 'amit', 'vivek', 'bharti', 'sejal', 'dimple']);
$chunks = $players->chunk(3);
 
print_r($chunks);
 
/*
Output:
 
Array
(
    [0] => Array
        (
            [0] => bhavik
            [1] => amit
            [2] => vivek
        )
 
    [1] => Array
        (
            [3] => bharti
            [4] => sejal
            [5] => dimple
        )
 
)
*/

Insert big data on the laravel

items array to collection:

$items = collect($items);

Make chunk

$chunks = $items->chunk(100);

foreach and insert by 100 each time

Read Also:  url download file - How to download a file from a URL?

foreach($chunks as $chunk){
    DB::table('items')->insert($chunk->toArray());
}

how to return chunk data laravel?

$count = 0;
DB::table('products')->chunk(200, function($products) use (&$count)
{
    Log::debug(count($products)); // will log the current iterations count
    $count = $count + count($products); // will write the total count to our method var
});
Log::debug($count); // will log the total count of records

laravel chunk select

DB::table('products')->chunk(100, function($products)
{
    foreach ($products as $product)
    {
        //
    }
});

get data from model in chunks laravel

$data = Inspector::latest('id')
    ->select('id', 'pcode', 'status', 'desc', 'price')
    ->where('pcode', 'LIKE', '%' . $searchtext . '%')
    ->chunk(50, function($inspectors) {
        foreach ($inspectors as $inspector) {
            // apply some action to the chunked results here
        }
    });

I hope you get an idea about laravel model attributes.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.