Articles

8 Best Laravel Collection Methods

by JONATHAN PAUL Jonathan Paul working at PHPProgrammers, a leading

This article is going to highlight some of the best Laravel Collection methods. Read on and make the most out of your Laravel application.

Other than returning eloquent results, the Laravel Collections can be used separately as well. To create a collection, you can simply pass an array to the collect helper function. The “Collections” contains some fruitful methods, which you can modify and filter. Here is a list of some the best Laravel Collection methods that are applicable to both the collections itself and the eloquent collections.

Here is how you can use the collect method to create a collection.



Here in this article, this array will be used for simplification.

When the helper methods are applied to the eloquent collections, these methods do not query the database for the results. First, all the results are taken from the databases. Then, the collection methods are used to modify and filter them without generating any query to the database.

#1 search()

This search method is used for searching the collection for a given value. The key value is returned if the the collection has the value. Otherwise, false is returned.



By default, the loose comparison is used for searching. If you want to use the strict comparison, the true argument can be passed to the search method as the second argument.


To the search method, you can pass your own callback function as well. The key of the first item, which passes the callback truth test will be returned by it.



#2 filter()

Using it, you can filter the collection with a callback. Only those items, which return true can be passed by it. Any other items that do not return true are removed. Without changing the original instance, this filter can return a new instance. The two parameters accepted by it in the callback are value and key.


The underlying array is returned by the all method. You will get the following response from the above code.


#3 chunk()

To split the collection into multiple collections of smaller size, you can use this chunk method. To display the collections into the grid, you can use it.


From the above code, you will get the following response.


#4 map()

To iterate through the entire collection, this method is used. As an argument, it accepts a callback. To the callback, the value and key are passed. The callback can modify the values and can return a new collection instance with the modified items.


It simply incremented the user_id by one.

From the above code, you will get the following response.


#5 dump()

This method is not only useful for debugging but also for finding what is inside the collection at any given point in the collection pipeline.


From the above code, you will get the following response.


#6 max()

With it, you can get the maximum value of a given key. By calling max(), you can get the maximum user_id. You can use it with strings as well. Using the following statement, you’ll get the maximum user_id.


#7 pluck()

If you want to extract values of one given column, use the pluck method as follows.


You’ll get a result as follows.


You can pass the column name as an argument while working with eloquent. This method can accept a second argument as well.

#8 pipe()

This method simply passes the collection to the callback and return the result. If you return a collection instance from the pipe callback, further methods can be chained as well. Here’s how you can use this method.


Other than all these aforementioned Laravel Collection methods, some other popular ones are

 

  • tap()
  • contains()
  • each()
  • whereNotIn()
  • zip()
  • avg()
  • forget()

 

You can hire Laravel experts to get the most out of your Laravel application using all these amazing Collection methods.

Jonathan Paul is a veteran Laravel developer having years of experience in Laravel development. He is presently working at PHPProgrammers, a Sydney-based Laravel development company from where you can hire Laravel programmers at affordable cost based on your project requirements.


Sponsor Ads


About JONATHAN PAUL Professional   Jonathan Paul working at PHPProgrammers, a leading

1,005 connections, 51 recommendations, 2,891 honor points.
Joined APSense since, April 20th, 2017, From Sydney, Australia.

Created on Oct 26th 2018 05:49. Viewed 497 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.