Knowledgebase

Working With Collections Print

  • phplaravel, database, php, performance, guide, howto, solution, zillionkinghost
  • 0

Handling sets of data.

WHAT A COLLECTION IS

A wrapper around an array with a large set of operations.

WHAT IT PROVIDES

Filtering, mapping, grouping, sorting, summing, and much more, chainable.

WHY IT MATTERS

It replaces loops with expressions describing intent.

WHAT ELOQUENT RETURNS

Collections, so these operations are available on query results.

WHAT TO BE CAREFUL WITH

Doing in a collection what the database should do.

WHY

Filtering thousands of records in PHP after loading them all is far slower than filtering in the query.

WHAT TO PUSH TO THE DATABASE

Filtering Sorting Aggregation Limiting

WHAT TO DO IN A COLLECTION

Transformation of results already retrieved Grouping for presentation

WHAT TO WATCH

Memory, on large collections.

WHAT TO USE FOR LARge VOLUMES

Lazy processing, which does not hold everything at once.

WHAT TO AVOID

Long chains nobody can follow Collections used where a simple loop is clearer


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot