Laravel - Collections
Laravel Collections are a powerful and expressive tool provided by the Laravel framework to work with arrays of data in a more convenient and fluent manner. Collections allow you to perform various operations on your data, such as filtering, transformation, aggregation, sorting, and more, using a chainable and easy-to-read syntax.
Creating a Collection:
You can create a collection in Laravel within your application code, typically in your controllers, models, or other parts of your application logic. Laravel provides the collect() helper function, which you can use to create a collection from an array or an existing collection. Here's how you can create a collection in different parts of your Laravel application:
1. Controller:
You can create a collection within a controller method. Here's an example of creating a collection from an array of data in a controller method:
2. Model:
You might create a collection within a model method. This is often useful when you're working with data fetched from a database or other data source:
3. Service or Helper Classes:
You can create collections in any custom service classes, helper functions, or other parts of your application's codebase:
Note:- Once you have a collection, you can chain various collection methods to perform operations on the data in a more expressive and efficient way. Collections are a versatile tool that can be used throughout your Laravel application to work with arrays of data more effectively.
There are various functions that you may perform once the collection is created. Some them are discuessed below.
Lets take an example of a collection
Common Collection Operations:
Filtering:
- filter(callback): Filter the collection based on a callback.
- where(key, operator, value): Filter the collection using a key-value comparison.
Transformation:
- map(callback): Transform each item in the collection using a callback.
- transform(callback): Modify the items in the collection using a callback.
- pluck(key): Extract a single column's values from the collection.
Aggregation:
- sum(key): Get the sum of the specified column's values.
- avg(key): Get the average of the specified column's values.
- max(key): Get the maximum value of the specified column.
- min(key): Get the minimum value of the specified column.
Sorting:
- sortBy(key): Sort the collection by the specified column.
- sortByDesc(key): Sort the collection in descending order by the specified column.
Grouping:
- groupBy(key): Group the collection's items by a given key.
Pagination:
- forPage(page, perPage): Get a new collection for a specific page and items per page.
Method Chaining:
One of the strengths of collections is the ability to chain methods together, creating a concise and readable sequence of operations:
Accessing Collection Data:
Collections implement the ArrayAccess interface, so you can access items using array notation:
There are many other Opertaions that you may perform on collections you may refer to official documentation Laravel for that.