How to optimize memory in laravel for best Perfomance

Sometimes, you will need to process thousand and thousand of records. You know that those operations are very heavy for your RAM, but Eloquent has a useful method to chunk query results in blocks to optimize your load.

  Apphouse::chunk(200, function($houses)
      foreach ($houses as $house)
          // heavy operations on the houses here...

The first parameter defines the size of the block you want to use. In this case, we will load 200 results, process them, unload them, and repeat the same thing with the next 200.

The second parameter is a closure that defines what to do with that chunk: the houses closure parameter is the returned collection of records.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest