collect method:
Working with Eloquent Collections
This opens up possibilities to create collections of Eloquent models:Working with Paginators
Or use a paginator:Internally the
from method of the data class will be used to create a new data object for each item in the collection.Collections Already Containing Data Objects
When the collection already contains data objects, thecollect method will return the same collection:
Type Transformation
The collect method also allows you to cast collections from one type into another. For example, you can pass in anarray and get back a Laravel collection:
This transformation will only work with non-paginator collections.
Magically Creating Collections
We’ve already seen thatfrom can create data objects magically. It is also possible to create a collection of data objects magically when using collect.
Let’s say you’ve implemented a custom collection class called SongCollection:
SongCollection will be returned:
Requirements for Magical Collection Methods
There are a few requirements for this to work:- The method must be static
- The method must be public
- The method must have a return type
- The method name must start with collect
- The method name must not be collect
Creating a Data Object with Collections
You can create a data object with a collection of data objects just like you would create a data object with a nested data object:Collection, the package will automatically convert the array into a collection of data objects.
DataCollections, PaginatedDataCollections and CursorPaginatedCollections
The package also provides a few collection classes which can be used to create collections of data objects. It was a requirement to use these classes in the past versions of the package when nesting data objects collections in data objects. This is no longer the case, but there are still valid use cases for them. You can create a DataCollection like this:Transforming paginated collection items
BothPaginatedDataCollection and CursorPaginatedDataCollection support the through() method to transform items in the collection:
through() method returns a new collection with transformed items while preserving pagination information.
This works identically for both
PaginatedDataCollection (using paginate()) and CursorPaginatedDataCollection (using cursorPaginate()).Why Use These Collection Classes?
We advise you to always use arrays, Laravel collections and paginators within your data objects. But let’s say you have a controller like this:This will only work when you’re using a
DataCollection, PaginatedDataCollection or CursorPaginatedCollection.DataCollection Methods
DataCollections provide some extra functionalities like:DataCollection class implements a few of the Laravel collection methods:
- through
- map
- filter
- first
- each
- values
- where
- reduce
- sole
The collection Method
In previous versions of the package it was possible to use the collection method to create a collection of data objects:
collect method. The collection method can still be used by using the WithDeprecatedCollectionMethod trait: