PASSWORD RESET

Your destination for complete Tech news

How to convert an array to a collection in Laravel?

629 0
< 1 min read

In Laravel, you can use the collect() helper function to convert an array into a collection. The collect() function creates a new collection instance from the given array.

Here is an example of how to use the collect() function to convert an array into a collection:

$array = [1, 2, 3, 4, 5];
$collection = collect($array);

Alternatively, you can use the new Collection constructor to create a new collection instance from an array:

$array = [1, 2, 3, 4, 5];
$collection = new Collection($array);

Once you have a collection instance, you can use the various methods available on the collection to manipulate and work with the data. For example, you can use the map() method to transform the items in the collection, or the sum() method to calculate the sum of the items in the collection.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.